
<!doctype html>
<html lang="en" class="no-js">

  <!-- Mirrored from flatbuffers.dev/tutorial/ by HTTrack Website Copier/3.x [XR&CO'2014], Thu, 08 May 2025 07:01:57 GMT -->
  <!-- Added by HTTrack --><meta http-equiv="content-type"
    content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">

    <link rel="canonical" href="index.html">

    <link rel="prev" href="../quick_start/index.html">

    <link rel="next" href="../building/index.html">

    <link rel="icon" href="../assets/images/favicon.png">
    <meta name="generator" content="mkdocs-1.6.1, mkdocs-material-9.6.9">

    <title>Tutorial - FlatBuffers Docs （文档）</title>

    <link rel="stylesheet" href="../assets/stylesheets/main.4af4bdda.min.css">

    <link rel="stylesheet"
      href="../assets/stylesheets/palette.06af60db.min.css">

    <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
    <link rel="stylesheet"
      href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&amp;display=fallback">
    <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>

    <script>__md_scope=new URL("..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>

  </head>

  <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="indigo"
    data-md-color-accent="indigo">

    <input class="md-toggle" data-md-toggle="drawer" type="checkbox"
      id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox"
      id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">

      <a href="#tutorial" class="md-skip">
        Skip to content
      </a>

    </div>
    <div data-md-component="announce">

    </div>

    <header class="md-header md-header--shadow" data-md-component="header">
      <nav class="md-header__inner md-grid" aria-label="Header">
        <a href="../index.html" title="FlatBuffers Docs （文档）"
          class="md-header__button md-logo" aria-label="FlatBuffers Docs （文档）"
          data-md-component="logo">

          <img src="../assets/flatbuffers_logo.svg" alt="logo">

        </a>
        <label class="md-header__button md-icon" for="__drawer">

          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path
              d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z" /></svg>
        </label>
        <div class="md-header__title" data-md-component="header-title">
          <div class="md-header__ellipsis">
            <div class="md-header__topic">
              <span class="md-ellipsis">
                FlatBuffers Docs （文档）
              </span>
            </div>
            <div class="md-header__topic" data-md-component="header-topic">
              <span class="md-ellipsis">

                Tutorial

              </span>
            </div>
          </div>
        </div>

        <form class="md-header__option" data-md-component="palette">

          <input class="md-option" data-md-color-media
            data-md-color-scheme="default" data-md-color-primary="indigo"
            data-md-color-accent="indigo" aria-label="Switch to dark mode"
            type="radio" name="__palette" id="__palette_0">

          <label class="md-header__button md-icon" title="Switch to dark mode"
            for="__palette_1" hidden>
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path
                d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z" /></svg>
          </label>

          <input class="md-option" data-md-color-media
            data-md-color-scheme="slate" data-md-color-primary="indigo"
            data-md-color-accent="indigo" aria-label="Switch to light mode"
            type="radio" name="__palette" id="__palette_1">

          <label class="md-header__button md-icon" title="Switch to light mode"
            for="__palette_0" hidden>
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path
                d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z" /></svg>
          </label>

        </form>

        <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script>

        <div class="md-header__source">
          <a href="https://github.com/google/flatbuffers"
            title="Go to repository" class="md-source"
            data-md-component="source">
            <div class="md-source__icon md-icon">

              <svg xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 496 512"><!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path
                  d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8M97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2" /></svg>
            </div>
            <div class="md-source__repository">
              google/FlatBuffers
            </div>
          </a>
        </div>

      </nav>

    </header>

    <div class="md-container" data-md-component="container">

      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">

          <div class="md-sidebar md-sidebar--primary"
            data-md-component="sidebar" data-md-type="navigation">
            <div class="md-sidebar__scrollwrap">
              <div class="md-sidebar__inner">

                <nav class="md-nav md-nav--primary" aria-label="Navigation"
                  data-md-level="0">
                  <label class="md-nav__title" for="__drawer">
                    <a href="../index.html" title="FlatBuffers Docs （文档）"
                      class="md-nav__button md-logo"
                      aria-label="FlatBuffers Docs （文档）"
                      data-md-component="logo">

                      <img src="../assets/flatbuffers_logo.svg" alt="logo">

                    </a>
                    FlatBuffers Docs （文档）
                  </label>

                  <div class="md-nav__source">
                    <a href="https://github.com/google/flatbuffers"
                      title="Go to repository" class="md-source"
                      data-md-component="source">
                      <div class="md-source__icon md-icon">

                        <svg xmlns="http://www.w3.org/2000/svg"
                          viewBox="0 0 496 512"><!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path
                            d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8M97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2" /></svg>
                      </div>
                      <div class="md-source__repository">
                        google/FlatBuffers
                      </div>
                    </a>
                  </div>

                  <ul class="md-nav__list" data-md-scrollfix>

                    <li class="md-nav__item">
                      <a href="../index.html" class="md-nav__link">

                        <span class="md-ellipsis">
                          Overview（概述）

                        </span>

                      </a>
                    </li>

                    <li class="md-nav__item">
                      <a href="../quick_start/index.html" class="md-nav__link">

                        <span class="md-ellipsis">
                          Quick Start（快速入门）

                        </span>

                      </a>
                    </li>

                    <li class="md-nav__item md-nav__item--active">

                      <input class="md-nav__toggle md-toggle" type="checkbox"
                        id="__toc">

                      <label class="md-nav__link md-nav__link--active"
                        for="__toc">

                        <span class="md-ellipsis">
                          Tutorial（教程）

                        </span>

                        <span class="md-nav__icon md-icon"></span>
                      </label>

                      <a href="index.html"
                        class="md-nav__link md-nav__link--active">

                        <span class="md-ellipsis">
                          Tutorial（教程）

                        </span>

                      </a>

                      <nav class="md-nav md-nav--secondary"
                        aria-label="Table of contents">

                        <label class="md-nav__title" for="__toc">
                          <span class="md-nav__icon md-icon"></span>
                          Table of contents
                        </label>
                        <ul class="md-nav__list" data-md-component="toc"
                          data-md-scrollfix>

                          <li class="md-nav__item">
                            <a href="#flatbuffers-schema-fbs"
                              class="md-nav__link">
                              <span class="md-ellipsis">
                                FlatBuffers Schema (.fbs)
                              </span>
                            </a>

                          </li>

                          <li class="md-nav__item">
                            <a href="#compiling-schema-to-code-flatc"
                              class="md-nav__link">
                              <span class="md-ellipsis">
                                Compiling Schema to Code (flatc)
                              </span>
                            </a>

                            <nav class="md-nav"
                              aria-label="Compiling Schema to Code (flatc)">
                              <ul class="md-nav__list">

                                <li class="md-nav__item">
                                  <a href="#building-flatc"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Building flatc
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#compiling-schema"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Compiling Schema
                                    </span>
                                  </a>

                                </li>

                              </ul>
                            </nav>

                          </li>

                          <li class="md-nav__item">
                            <a href="#application-integration"
                              class="md-nav__link">
                              <span class="md-ellipsis">
                                Application Integration
                              </span>
                            </a>

                          </li>

                          <li class="md-nav__item">
                            <a href="#serialization" class="md-nav__link">
                              <span class="md-ellipsis">
                                Serialization
                              </span>
                            </a>

                            <nav class="md-nav" aria-label="Serialization">
                              <ul class="md-nav__list">

                                <li class="md-nav__item">
                                  <a href="#flatbufferbuilder"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      FlatBufferBuilder
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#serializing-data"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Serializing Data
                                    </span>
                                  </a>

                                  <nav class="md-nav"
                                    aria-label="Serializing Data">
                                    <ul class="md-nav__list">

                                      <li class="md-nav__item">
                                        <a href="#strings" class="md-nav__link">
                                          <span class="md-ellipsis">
                                            Strings
                                          </span>
                                        </a>

                                      </li>

                                      <li class="md-nav__item">
                                        <a href="#tables" class="md-nav__link">
                                          <span class="md-ellipsis">
                                            Tables
                                          </span>
                                        </a>

                                      </li>

                                      <li class="md-nav__item">
                                        <a href="#vectors" class="md-nav__link">
                                          <span class="md-ellipsis">
                                            Vectors
                                          </span>
                                        </a>

                                      </li>

                                      <li class="md-nav__item">
                                        <a href="#unions" class="md-nav__link">
                                          <span class="md-ellipsis">
                                            Unions
                                          </span>
                                        </a>

                                      </li>

                                    </ul>
                                  </nav>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#finishing" class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Finishing
                                    </span>
                                  </a>

                                  <nav class="md-nav" aria-label="Finishing">
                                    <ul class="md-nav__list">

                                      <li class="md-nav__item">
                                        <a href="#buffer-access"
                                          class="md-nav__link">
                                          <span class="md-ellipsis">
                                            Buffer Access
                                          </span>
                                        </a>

                                      </li>

                                    </ul>
                                  </nav>

                                </li>

                              </ul>
                            </nav>

                          </li>

                          <li class="md-nav__item">
                            <a href="#deserialization" class="md-nav__link">
                              <span class="md-ellipsis">
                                Deserialization
                              </span>
                            </a>

                            <nav class="md-nav" aria-label="Deserialization">
                              <ul class="md-nav__list">

                                <li class="md-nav__item">
                                  <a href="#root-access" class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Root Access
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#table-access" class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Table Access
                                    </span>
                                  </a>

                                  <nav class="md-nav" aria-label="Table Access">
                                    <ul class="md-nav__list">

                                      <li class="md-nav__item">
                                        <a href="#nested-object-access"
                                          class="md-nav__link">
                                          <span class="md-ellipsis">
                                            Nested Object Access
                                          </span>
                                        </a>

                                      </li>

                                    </ul>
                                  </nav>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#vector-access" class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Vector Access
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#union-access" class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Union Access
                                    </span>
                                  </a>

                                </li>

                              </ul>
                            </nav>

                          </li>

                        </ul>

                      </nav>

                    </li>

                    <li class="md-nav__item md-nav__item--nested">

                      <input
                        class="md-nav__toggle md-toggle md-toggle--indeterminate"
                        type="checkbox" id="__nav_4">

                      <label class="md-nav__link" for="__nav_4"
                        id="__nav_4_label" tabindex="0">

                        <span class="md-ellipsis">
                          Compiler (flatc)（编译器）

                        </span>

                        <span class="md-nav__icon md-icon"></span>
                      </label>

                      <nav class="md-nav" data-md-level="1"
                        aria-labelledby="__nav_4_label" aria-expanded="false">
                        <label class="md-nav__title" for="__nav_4">
                          <span class="md-nav__icon md-icon"></span>
                          Compiler (flatc)
                        </label>
                        <ul class="md-nav__list" data-md-scrollfix>

                          <li class="md-nav__item">
                            <a href="../building/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Building（构建）

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../flatc/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                Using（使用）

                              </span>

                            </a>
                          </li>

                        </ul>
                      </nav>

                    </li>

                    <li class="md-nav__item md-nav__item--nested">

                      <input
                        class="md-nav__toggle md-toggle md-toggle--indeterminate"
                        type="checkbox" id="__nav_5">

                      <label class="md-nav__link" for="__nav_5"
                        id="__nav_5_label" tabindex="0">

                        <span class="md-ellipsis">
                          Schema (.fbs)（模式定义）

                        </span>

                        <span class="md-nav__icon md-icon"></span>
                      </label>

                      <nav class="md-nav" data-md-level="1"
                        aria-labelledby="__nav_5_label" aria-expanded="false">
                        <label class="md-nav__title" for="__nav_5">
                          <span class="md-nav__icon md-icon"></span>
                          Schema (.fbs)
                        </label>
                        <ul class="md-nav__list" data-md-scrollfix>

                          <li class="md-nav__item">
                            <a href="../schema/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                Overview（概述）

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../evolution/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Evolution（演变）

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../grammar/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Grammar（语法）

                              </span>

                            </a>
                          </li>

                        </ul>
                      </nav>

                    </li>

                    <li class="md-nav__item md-nav__item--nested">

                      <input
                        class="md-nav__toggle md-toggle md-toggle--indeterminate"
                        type="checkbox" id="__nav_6">

                      <label class="md-nav__link" for="__nav_6"
                        id="__nav_6_label" tabindex="0">

                        <span class="md-ellipsis">
                          Language Guides（语言指南）

                        </span>

                        <span class="md-nav__icon md-icon"></span>
                      </label>

                      <nav class="md-nav" data-md-level="1"
                        aria-labelledby="__nav_6_label" aria-expanded="false">
                        <label class="md-nav__title" for="__nav_6">
                          <span class="md-nav__icon md-icon"></span>
                          Language Guides
                        </label>
                        <ul class="md-nav__list" data-md-scrollfix>

                          <li class="md-nav__item">
                            <a href="../languages/c/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                C

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/cpp/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                C++

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/c_sharp/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                C#

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/dart/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Dart

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/go/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Go

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/java/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Java

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/javascript/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                JavasScript

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/kotlin/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Kotlin

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/lobster/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Lobster

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/lua/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Lua

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/php/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                PHP

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/python/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Python

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/rust/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Rust

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/swift/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Swift

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../languages/typescript/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                TypeScript

                              </span>

                            </a>
                          </li>

                        </ul>
                      </nav>

                    </li>

                    <li class="md-nav__item">
                      <a href="../support/index.html" class="md-nav__link">

                        <span class="md-ellipsis">
                          Supported Configurations（支持的配置）

                        </span>

                      </a>
                    </li>

                    <li class="md-nav__item">
                      <a href="../white_paper/index.html" class="md-nav__link">

                        <span class="md-ellipsis">
                          White Paper（白皮书）

                        </span>

                      </a>
                    </li>

                    <li class="md-nav__item md-nav__item--nested">

                      <input
                        class="md-nav__toggle md-toggle md-toggle--indeterminate"
                        type="checkbox" id="__nav_9">

                      <label class="md-nav__link" for="__nav_9"
                        id="__nav_9_label" tabindex="0">

                        <span class="md-ellipsis">
                          Advanced（高级）

                        </span>

                        <span class="md-nav__icon md-icon"></span>
                      </label>

                      <nav class="md-nav" data-md-level="1"
                        aria-labelledby="__nav_9_label" aria-expanded="false">
                        <label class="md-nav__title" for="__nav_9">
                          <span class="md-nav__icon md-icon"></span>
                          Advanced
                        </label>
                        <ul class="md-nav__list" data-md-scrollfix>

                          <li class="md-nav__item">
                            <a href="../internals/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                FlatBuffers Internals（内部结构）

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../intermediate_representation/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Intermediate Representation（中间表示）

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../annotation/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Annotating Buffers (.afb)

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../benchmarks/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Benchmarks（基准测试）

                              </span>

                            </a>
                          </li>

                        </ul>
                      </nav>

                    </li>

                    <li class="md-nav__item">
                      <a href="../flexbuffers/index.html" class="md-nav__link">

                        <span class="md-ellipsis">
                          FlexBuffers (Schema-less version)（无模式版本）

                        </span>

                      </a>
                    </li>

                    <li class="md-nav__item">
                      <a href="../contributing/index.html" class="md-nav__link">

                        <span class="md-ellipsis">
                          Contributing（贡献）

                        </span>

                      </a>
                    </li>

                  </ul>
                </nav>
              </div>
            </div>
          </div>

          <div class="md-sidebar md-sidebar--secondary"
            data-md-component="sidebar" data-md-type="toc">
            <div class="md-sidebar__scrollwrap">
              <div class="md-sidebar__inner">

                <nav class="md-nav md-nav--secondary"
                  aria-label="Table of contents">

                  <label class="md-nav__title" for="__toc">
                    <span class="md-nav__icon md-icon"></span>
                    Table of contents
                  </label>
                  <ul class="md-nav__list" data-md-component="toc"
                    data-md-scrollfix>

                    <li class="md-nav__item">
                      <a href="#flatbuffers-schema-fbs" class="md-nav__link">
                        <span class="md-ellipsis">
                          FlatBuffers Schema (.fbs)
                        </span>
                      </a>

                    </li>

                    <li class="md-nav__item">
                      <a href="#compiling-schema-to-code-flatc"
                        class="md-nav__link">
                        <span class="md-ellipsis">
                          Compiling Schema to Code (flatc)
                        </span>
                      </a>

                      <nav class="md-nav"
                        aria-label="Compiling Schema to Code (flatc)">
                        <ul class="md-nav__list">

                          <li class="md-nav__item">
                            <a href="#building-flatc" class="md-nav__link">
                              <span class="md-ellipsis">
                                Building flatc
                              </span>
                            </a>

                          </li>

                          <li class="md-nav__item">
                            <a href="#compiling-schema" class="md-nav__link">
                              <span class="md-ellipsis">
                                Compiling Schema
                              </span>
                            </a>

                          </li>

                        </ul>
                      </nav>

                    </li>

                    <li class="md-nav__item">
                      <a href="#application-integration" class="md-nav__link">
                        <span class="md-ellipsis">
                          Application Integration
                        </span>
                      </a>

                    </li>

                    <li class="md-nav__item">
                      <a href="#serialization" class="md-nav__link">
                        <span class="md-ellipsis">
                          Serialization
                        </span>
                      </a>

                      <nav class="md-nav" aria-label="Serialization">
                        <ul class="md-nav__list">

                          <li class="md-nav__item">
                            <a href="#flatbufferbuilder" class="md-nav__link">
                              <span class="md-ellipsis">
                                FlatBufferBuilder
                              </span>
                            </a>

                          </li>

                          <li class="md-nav__item">
                            <a href="#serializing-data" class="md-nav__link">
                              <span class="md-ellipsis">
                                Serializing Data
                              </span>
                            </a>

                            <nav class="md-nav" aria-label="Serializing Data">
                              <ul class="md-nav__list">

                                <li class="md-nav__item">
                                  <a href="#strings" class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Strings
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#tables" class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Tables
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#vectors" class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Vectors
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#unions" class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Unions
                                    </span>
                                  </a>

                                </li>

                              </ul>
                            </nav>

                          </li>

                          <li class="md-nav__item">
                            <a href="#finishing" class="md-nav__link">
                              <span class="md-ellipsis">
                                Finishing
                              </span>
                            </a>

                            <nav class="md-nav" aria-label="Finishing">
                              <ul class="md-nav__list">

                                <li class="md-nav__item">
                                  <a href="#buffer-access" class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Buffer Access
                                    </span>
                                  </a>

                                </li>

                              </ul>
                            </nav>

                          </li>

                        </ul>
                      </nav>

                    </li>

                    <li class="md-nav__item">
                      <a href="#deserialization" class="md-nav__link">
                        <span class="md-ellipsis">
                          Deserialization
                        </span>
                      </a>

                      <nav class="md-nav" aria-label="Deserialization">
                        <ul class="md-nav__list">

                          <li class="md-nav__item">
                            <a href="#root-access" class="md-nav__link">
                              <span class="md-ellipsis">
                                Root Access
                              </span>
                            </a>

                          </li>

                          <li class="md-nav__item">
                            <a href="#table-access" class="md-nav__link">
                              <span class="md-ellipsis">
                                Table Access
                              </span>
                            </a>

                            <nav class="md-nav" aria-label="Table Access">
                              <ul class="md-nav__list">

                                <li class="md-nav__item">
                                  <a href="#nested-object-access"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      Nested Object Access
                                    </span>
                                  </a>

                                </li>

                              </ul>
                            </nav>

                          </li>

                          <li class="md-nav__item">
                            <a href="#vector-access" class="md-nav__link">
                              <span class="md-ellipsis">
                                Vector Access
                              </span>
                            </a>

                          </li>

                          <li class="md-nav__item">
                            <a href="#union-access" class="md-nav__link">
                              <span class="md-ellipsis">
                                Union Access
                              </span>
                            </a>

                          </li>

                        </ul>
                      </nav>

                    </li>

                  </ul>

                </nav>
              </div>
            </div>
          </div>

          <div class="md-content" data-md-component="content">
            <article class="md-content__inner md-typeset">

              <a
                href="https://github.com/google/flatbuffers/edit/master/docs/source/tutorial.md"
                title="Edit this page" class="md-content__button md-icon">

                <svg xmlns="http://www.w3.org/2000/svg"
                  viewBox="0 0 24 24"><path
                    d="M10 20H6V4h7v5h5v3.1l2-2V8l-6-6H6c-1.1 0-2 .9-2 2v16c0 1.1.9 2 2 2h4zm10.2-7c.1 0 .3.1.4.2l1.3 1.3c.2.2.2.6 0 .8l-1 1-2.1-2.1 1-1c.1-.1.2-.2.4-.2m0 3.9L14.1 23H12v-2.1l6.1-6.1z" /></svg>
              </a>

              <h1 id="tutorial">Tutorial（教程）</h1>
              <p>本教程提供了一个如何在多种语言中使用 FlatBuffers 的示例。以下主题将涵盖在你的应用程序中使用
                FlatBuffers 的所有步骤。</p>
              <ol>
                <li>编写 FlatBuffers schema 文件（<code>.fbs</code>）。</li>
                <li>使用 <code>flatc</code> 编译器将 schema 转换为特定语言的代码。</li>
                <li>将生成的代码和库导入到你的应用程序中。</li>
                <li>将数据序列化为 flatbuffer。</li>
                <li>反序列化 flatbuffer。</li>
              </ol>
              <p>本教程结构上是与语言无关的，具体语言的内容会在代码块中提供更多上下文。此外，这里尝试涵盖 FlatBuffers
                的主要部分和类型系统，以便给出一个总体 Overview（概述）。它并不旨在穷尽所有功能，也不一定提供最佳实践。</p>
              <h2 id="flatbuffers-schema-fbs">FlatBuffers
                Schema（模式定义）（<code>.fbs</code>）</h2>
              <p>要开始使用 FlatBuffers，首先需要创建一个
                <a href="../schema/index.html">schema（模式）</a>
                文件，该文件定义了你希望序列化的数据结构的格式。该模式会被 <code>flatc</code>
                编译器处理，生成你在项目中使用的特定语言代码。</p>
              <p>本教程将使用以下的
                <a
                  href="https://github.com/google/flatbuffers/blob/master/samples/monster.fbs"><code>monster.fbs（monster.fbs
                    示例文件）</code></a>
                模式。它是 FlatBuffers
                <a
                  href="https://github.com/google/flatbuffers/tree/master/samples">sample
                  code（示例代码）</a> 的一部分，用于提供完整的示例二进制演示。</p>
              <p>FlatBuffers schema（模式）是一种 Interface Definition
                Language（接口定义语言，IDL），包含一些数据结构，详细描述请参见
                <a href="../schema/index.html">schema（模式）</a>
                文档。你可以通过内联代码注释快速了解模式的每个部分。</p>
              <div class="highlight"><table class="highlighttable"><tr><th
                      colspan="2" class="filename"><span
                        class="filename">monster.fbs</span></th></tr><tr><td
                      class="linenos"><div
                        class="linenodiv"><pre><span></span><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span>
<span class="normal">16</span>
<span class="normal">17</span>
<span class="normal">18</span>
<span class="normal">19</span>
<span class="normal">20</span>
<span class="normal">21</span>
<span class="normal">22</span>
<span class="normal">23</span>
<span class="normal">24</span>
<span class="normal">25</span>
<span class="normal">26</span>
<span class="normal">27</span>
<span class="normal">28</span>
<span class="normal">29</span>
<span class="normal">30</span>
<span class="normal">31</span>
<span class="normal">32</span>
<span class="normal">33</span>
<span class="normal">34</span></pre></div></td><td
                      class="code"><div><pre><span></span><code><span class="c1">// Example IDL file for our monster&#39;s schema.</span>

<span class="n">namespace</span><span class="w"> </span><span class="n">MyGame.Sample</span><span class="p">;</span><span class="w"> </span><span class="c1">//(1)!</span>

<span class="kd">enum</span><span class="w"> </span><span class="n">Color</span><span class="o">:</span><span class="n">byte</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="na">Red</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">Green</span><span class="p">,</span><span class="w"> </span><span class="na">Blue</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">//(2)!</span>

<span class="c1">// Optionally add more tables.</span>
<span class="n">union</span><span class="w"> </span><span class="n">Equipment</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">Weapon</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">//(3)!</span>

<span class="n">struct</span><span class="w"> </span><span class="n">Vec3</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">//(4)!</span>
<span class="w">  </span><span class="n">x</span><span class="o">:</span><span class="kt">float</span><span class="p">;</span><span class="w"> </span><span class="c1">//(5)!</span>
<span class="w">  </span><span class="n">y</span><span class="o">:</span><span class="kt">float</span><span class="p">;</span>
<span class="w">  </span><span class="n">z</span><span class="o">:</span><span class="kt">float</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">table</span><span class="w"> </span><span class="n">Monster</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">//(6)!</span>
<span class="w">  </span><span class="n">pos</span><span class="o">:</span><span class="n">Vec3</span><span class="p">;</span><span class="w"> </span><span class="c1">//(7)!</span>
<span class="w">  </span><span class="n">mana</span><span class="o">:</span><span class="na">short</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">150</span><span class="p">;</span><span class="w"> </span><span class="c1">//(8)!</span>
<span class="w">  </span><span class="n">hp</span><span class="o">:</span><span class="na">short</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">100</span><span class="p">;</span>
<span class="w">  </span><span class="n">name</span><span class="o">:</span><span class="kt">string</span><span class="p">;</span><span class="w"> </span><span class="c1">//(9)!</span>
<span class="w">  </span><span class="n">friendly</span><span class="o">:</span><span class="kt">bool</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">(</span><span class="n">deprecated</span><span class="p">);</span><span class="w"> </span><span class="c1">//(10)!</span>
<span class="w">  </span><span class="n">inventory</span><span class="o">:</span><span class="p">[</span><span class="n">ubyte</span><span class="p">];</span><span class="w"> </span><span class="c1">//(11)!</span>
<span class="w">  </span><span class="n">color</span><span class="o">:</span><span class="na">Color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Blue</span><span class="p">;</span>
<span class="w">  </span><span class="n">weapons</span><span class="o">:</span><span class="p">[</span><span class="n">Weapon</span><span class="p">];</span><span class="w"> </span><span class="c1">//(12)!</span>
<span class="w">  </span><span class="n">equipped</span><span class="o">:</span><span class="n">Equipment</span><span class="p">;</span><span class="w"> </span><span class="c1">//(13)!</span>
<span class="w">  </span><span class="n">path</span><span class="o">:</span><span class="p">[</span><span class="n">Vec3</span><span class="p">];</span>
<span class="p">}</span>

<span class="n">table</span><span class="w"> </span><span class="n">Weapon</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="n">name</span><span class="o">:</span><span class="kt">string</span><span class="p">;</span>
<span class="w">  </span><span class="n">damage</span><span class="o">:</span><span class="n">short</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">root_type</span><span class="w"> </span><span class="n">Monster</span><span class="p">;</span><span class="w"> </span><span class="c1">//(14)!</span>
</code></pre></div></td></tr></table></div>
              <ol>
                <li>
                  <p>FlatBuffers has support for namespaces to place the
                    generated code into.
                    There is mixed level of support for namespaces (some
                    languages don't have
                    namespaces), but for the C family of languages, it is fully
                    supported.</p>
                </li>
                <li>
                  <p>Enums definitions can be defined with the backing numerical
                    type. Implicit
                    numbering is supported, so that <code>Green</code> would
                    have a value of 1.</p>
                </li>
                <li>
                  <p>A union represents a single value from a set of possible
                    values. Its
                    effectively an enum (to represent the type actually store)
                    and a value,
                    combined into one. In this example, the union is not very
                    useful, since it
                    only has a single type.</p>
                </li>
                <li>
                  <p>A struct is a collection of scalar fields with names. It is
                    itself a scalar
                    type, which uses less memory and has faster lookup. However,
                    once a struct is
                    defined, it cannot be changed. Use tables for data
                    structures that can evolve
                    over time.</p>
                </li>
                <li>
                  <p>FlatBuffers has the standard set of scalar numerical types
                    (<code>int8</code>, <code>int16</code>,
                    <code>int32</code>, <code>int64</code>, <code>uint8</code>,
                    <code>uint16</code>, <code>uint32</code>,
                    <code>uint64</code>, <code>float</code>,
                    <code>double</code>),
                    as well as <code>bool</code>. Note, scalars are fixed width,
                    <code>varints</code> are not
                    supported.</p>
                </li>
                <li>
                  <p>Tables are the main data structure for grouping data
                    together. It can evolve
                    by adding and deprecating fields over time, while preserving
                    forward and
                    backwards compatibility.</p>
                </li>
                <li>
                  <p>A field that happens to be a <code>struct</code>. This
                    means the data of the <code>Vec3</code>
                    struct will be serialized inline in the table without any
                    need for offset.</p>
                </li>
                <li>
                  <p>Fields can be provided a default value. Default values can
                    be configured to
                    not be serialized at all while still providing the default
                    value while
                    deserializing. However, once set, a default value cannot be
                    changed.</p>
                </li>
                <li>
                  <p>A <code>string</code> field which points to a serialized
                    string external to the table.</p>
                </li>
                <li>
                  <p>A deprecated field that is no longer being used. This is
                    used instead of
                    removing the field outright.</p>
                </li>
                <li>
                  <p>A <code>vector</code> field that points to a vector of
                    bytes. Like <code>strings</code>, the
                    vector data is serialized elsewhere and this field just
                    stores an offset to
                    the vector.</p>
                </li>
                <li>
                  <p>Vector of <code>tables</code> and <code>structs</code> are
                    also possible.</p>
                </li>
                <li>
                  <p>A field to a <code>union</code> type.</p>
                </li>
                <li>
                  <p>The root of the flatbuffer is always a <code>table</code>.
                    This indicates the type of
                    <code>table</code> the "entry" point of the flatbuffer will
                    point to.</p>
                </li>
              </ol>
              <h2 id="compiling-schema-to-code-flatc">Compiling Schema to
                Code（将模式编译为代码） (<code>flatc</code>)</h2>
              <p>在编写好 schema（模式）文件后，你可以将其编译为你希望使用的各种语言的代码。此编译过程由 <a
                  href="../flatc/index.html">FlatBuffers Compiler（FlatBuffers
                  编译器）</a>（<code>flatc</code>）完成，<code>flatc</code>
                是该仓库中构建出的二进制文件之一。</p>
              <h3 id="building-flatc">Building <code>flatc</code>（构建
                <code>flatc</code>）</h3>
              <p>FlatBuffers 使用 <a
                  href="https://cmake.org/"><code>cmake</code>（CMake 构建工具）</a>
                来为您的环境生成项目文件。</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="1:2"><input
                  checked="checked" id="unix" name="__tabbed_1"
                  type="radio" /><input id="windows" name="__tabbed_1"
                  type="radio" /><div class="tabbed-labels"><label
                    for="unix">Unix</label><label
                    for="windows">Windows</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>cmake<span class="w"> </span>-G<span class="w"> </span><span class="s2">&quot;Unix Makefiles&quot;</span>
make<span class="w"> </span>flatc
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>cmake<span class="w"> </span>-G<span class="w"> </span><span class="s2">&quot;Visual Studio 17 2022&quot;</span>
msbuild.exe<span class="w"> </span>FlatBuffers.sln
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>有关更多详细信息及其他环境的说明，请参阅<a
                  href="../building/index.html">building（构建）</a>文档。一些编程语言还可以通过其包管理器直接获取预编译的
                <code>flatc</code>。</p>
              <h3 id="compiling-schema">Compiling Schema（编译 Schema）</h3>
              <p>要编译 schema（模式文件），请使用 <code>flatc</code> 命令，并指定 schema
                文件以及您希望生成代码的语言标志。此编译过程将生成可包含在您的应用程序代码中的文件。这些文件为序列化和反序列化
                flatbuffer（二进制缓冲区）数据提供了便捷的 API。</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="2:15"><input
                  checked="checked" id="c" name="__tabbed_2"
                  type="radio" /><input id="c_1" name="__tabbed_2"
                  type="radio" /><input id="c_2" name="__tabbed_2"
                  type="radio" /><input id="dart" name="__tabbed_2"
                  type="radio" /><input id="go" name="__tabbed_2"
                  type="radio" /><input id="java" name="__tabbed_2"
                  type="radio" /><input id="javascript" name="__tabbed_2"
                  type="radio" /><input id="kotlin" name="__tabbed_2"
                  type="radio" /><input id="lobster" name="__tabbed_2"
                  type="radio" /><input id="lua" name="__tabbed_2"
                  type="radio" /><input id="php" name="__tabbed_2"
                  type="radio" /><input id="python" name="__tabbed_2"
                  type="radio" /><input id="rust" name="__tabbed_2"
                  type="radio" /><input id="swift" name="__tabbed_2"
                  type="radio" /><input id="typescript" name="__tabbed_2"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c">C++</label><label for="c_1">C</label><label
                    for="c_2">C#</label><label for="dart">Dart</label><label
                    for="go">Go</label><label for="java">Java</label><label
                    for="javascript">JavaScript</label><label
                    for="kotlin">Kotlin</label><label
                    for="lobster">Lobster</label><label
                    for="lua">Lua</label><label for="php">PHP</label><label
                    for="python">Python</label><label
                    for="rust">Rust</label><label
                    for="swift">Swift</label><label
                    for="typescript">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--cpp<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div class="admonition note">
                      <p class="admonition-title">Note（注意）</p>
                      <p>如果您在使用 C 语言进行开发，需要使用单独的项目 <a
                          href="https://github.com/dvidelabs/flatcc">FlatCC（FlatCC
                          项目）</a>，该项目为 C 语言提供了 schema（模式文件）编译器和运行时库。请参阅 <a
                          href="https://github.com/dvidelabs/flatcc#building">flatcc
                          build instructions（flatcc 构建说明）</a>。</p>
                      <p>请注意 <code>flatc</code> 和 <code>flatcc</code>
                        工具之间的区别。</p>
                    </div>
                    <div
                      class="highlight"><pre><span></span><code><span class="nb">cd</span><span class="w"> </span>flatcc
mkdir<span class="w"> </span>-p<span class="w"> </span>build/tmp/samples/monster
bin/flatcc<span class="w"> </span>-a<span class="w"> </span>-o<span class="w"> </span>build/tmp/samples/monster<span class="w"> </span>samples/monster/monster.fbs
<span class="c1"># or just</span>
flatcc/samples/monster/build.sh
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--csharp<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--dart<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--go<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--java<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--js<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--kotlin<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--lobster<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--lua<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--php<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--python<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--rust<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--swift<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>flatc<span class="w"> </span>--ts<span class="w"> </span>monster.fbs
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>您可以在与序列化
                flatbuffers（二进制缓冲区）时所用语言不同的语言中进行反序列化。为了本教程的目的，我们假设序列化和反序列化均使用同一种语言。</p>
              <h2 id="application-integration">Application
                Integration（应用集成）</h2>
              <p>生成的文件随后会被包含到您的项目中，并构建进您的应用程序。这一过程在很大程度上依赖于您的构建系统和编程语言，但通常涉及以下两个方面：</p>
              <ol>
                <li>导入生成的代码。</li>
                <li>导入“runtime（运行时）”库。</li>
              </ol>
              <div class="tabbed-set tabbed-alternate" data-tabs="3:15"><input
                  checked="checked" id="c_3" name="__tabbed_3"
                  type="radio" /><input id="c_4" name="__tabbed_3"
                  type="radio" /><input id="c_5" name="__tabbed_3"
                  type="radio" /><input id="dart_1" name="__tabbed_3"
                  type="radio" /><input id="go_1" name="__tabbed_3"
                  type="radio" /><input id="java_1" name="__tabbed_3"
                  type="radio" /><input id="javascript_1" name="__tabbed_3"
                  type="radio" /><input id="kotlin_1" name="__tabbed_3"
                  type="radio" /><input id="lobster_1" name="__tabbed_3"
                  type="radio" /><input id="lua_1" name="__tabbed_3"
                  type="radio" /><input id="php_1" name="__tabbed_3"
                  type="radio" /><input id="python_1" name="__tabbed_3"
                  type="radio" /><input id="rust_1" name="__tabbed_3"
                  type="radio" /><input id="swift_1" name="__tabbed_3"
                  type="radio" /><input id="typescript_1" name="__tabbed_3"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_3">C++</label><label for="c_4">C</label><label
                    for="c_5">C#</label><label for="dart_1">Dart</label><label
                    for="go_1">Go</label><label for="java_1">Java</label><label
                    for="javascript_1">JavaScript</label><label
                    for="kotlin_1">Kotlin</label><label
                    for="lobster_1">Lobster</label><label
                    for="lua_1">Lua</label><label for="php_1">PHP</label><label
                    for="python_1">Python</label><label
                    for="rust_1">Rust</label><label
                    for="swift_1">Swift</label><label
                    for="typescript_1">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;monster_generated.h&quot;</span><span class="c1"> // This was generated by `flatc`</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;flatbuffers.h&quot;</span><span class="c1"> // The runtime library for C++</span>

<span class="c1">// Simplifies naming in the following examples.</span>
<span class="k">using</span><span class="w"> </span><span class="k">namespace</span><span class="w"> </span><span class="nn">MyGame</span><span class="o">::</span><span class="nn">Sample</span><span class="p">;</span><span class="w"> </span><span class="c1">// Specified in the schema.</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;monster_builder.h&quot;</span><span class="c1"> // Generated by `flatcc`.</span>

<span class="c1">// Convenient namespace macro to manage long namespace prefix.</span>
<span class="cp">#undef ns</span>
<span class="c1">// Specified in the schema.</span>
<span class="cp">#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(MyGame_Sample, x) </span>

<span class="c1">// A helper to simplify creating vectors from C-arrays.</span>
<span class="cp">#define c_vec_len(V) (sizeof(V)/sizeof((V)[0]))</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="k">using</span><span class="w"> </span><span class="nn">Google.FlatBuffers</span><span class="p">;</span><span class="w"> </span><span class="c1">// The runtime library for C#</span>
<span class="k">using</span><span class="w"> </span><span class="nn">MyGame.Sample</span><span class="p">;</span><span class="w"> </span><span class="c1">// The generated files from `flatc`</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="k">import</span><span class="w"> </span><span class="s1">&#39;package:flat_buffers/flat_buffers.dart&#39;</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">fb</span><span class="p">;</span>

<span class="c1">// Generated by `flatc`.</span>
<span class="k">import</span><span class="w"> </span><span class="s1">&#39;monster_my_game.sample_generated.dart&#39;</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">myGame</span><span class="p">;</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="p">(</span>
<span class="w">      </span><span class="nx">flatbuffers</span><span class="w"> </span><span class="s">&quot;github.com/google/flatbuffers/go&quot;</span>
<span class="w">      </span><span class="nx">sample</span><span class="w"> </span><span class="s">&quot;MyGame/Sample&quot;</span>
<span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">MyGame.Sample.*</span><span class="p">;</span><span class="w"> </span><span class="c1">//The `flatc` generated files. (Monster, Vec3, etc.)</span>

<span class="kn">import</span><span class="w"> </span><span class="nn">com.google.flatbuffers.FlatBufferBuilder</span><span class="p">;</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// The following code is an example - use your desired module flavor by </span>
<span class="c1">// transpiling from TS. </span>
<span class="kd">var</span><span class="w"> </span><span class="nx">flatbuffers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">require</span><span class="p">(</span><span class="s1">&#39;/js/flatbuffers&#39;</span><span class="p">).</span><span class="nx">flatbuffers</span><span class="p">;</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">MyGame</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./monster_generated&#39;</span><span class="p">).</span><span class="nx">MyGame</span><span class="p">;</span><span class="w"> </span><span class="c1">// Generated by `flatc`.</span>

<span class="c1">//------------------------------------------------------------------------//</span>

<span class="c1">// The following code is for browser-based HTML/JavaScript. Use the above </span>
<span class="c1">// code for JavaScript module loaders (e.g. Node.js).</span>
<span class="o">&lt;</span><span class="nx">script</span><span class="w"> </span><span class="nx">src</span><span class="o">=</span><span class="s2">&quot;../js/flatbuffers.js&quot;</span><span class="o">&gt;&lt;</span><span class="err">/script&gt;</span>
<span class="o">&lt;</span><span class="nx">script</span><span class="w"> </span><span class="nx">src</span><span class="o">=</span><span class="s2">&quot;monster_generated.js&quot;</span><span class="o">&gt;&lt;</span><span class="sr">/script&gt; /</span><span class="o">/</span><span class="w"> </span><span class="nx">Generated</span><span class="w"> </span><span class="nx">by</span><span class="w"> </span><span class="sb">`flatc`</span><span class="p">.</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="k">import</span><span class="w"> </span><span class="nn">MyGame.Sample.*</span><span class="w"> </span><span class="c1">//The `flatc` generated files. (Monster, Vec3, etc.)</span>

<span class="k">import</span><span class="w"> </span><span class="nn">com.google.flatbuffers.FlatBufferBuilder</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>import from &quot;../lobster/&quot;  // Where to find flatbuffers.lobster
import monster_generated
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">-- require the flatbuffers module</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">flatbuffers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">require</span><span class="p">(</span><span class="s2">&quot;flatbuffers&quot;</span><span class="p">)</span>

<span class="c1">-- require the generated files from `flatc`.</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">require</span><span class="p">(</span><span class="s2">&quot;MyGame.Sample.Color&quot;</span><span class="p">)</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">equipment</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">require</span><span class="p">(</span><span class="s2">&quot;MyGame.Sample.Equipment&quot;</span><span class="p">)</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">require</span><span class="p">(</span><span class="s2">&quot;MyGame.Sample.Monster&quot;</span><span class="p">)</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">vec3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">require</span><span class="p">(</span><span class="s2">&quot;MyGame.Sample.Vec3&quot;</span><span class="p">)</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">weapon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">require</span><span class="p">(</span><span class="s2">&quot;MyGame.Sample.Weapon&quot;</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// It is recommended that your use PSR autoload when using FlatBuffers in</span>
<span class="c1">// PHP. Here is an example from `SampleBinary.php`:</span>
<span class="k">function</span> <span class="nf">__autoload</span><span class="p">(</span><span class="nv">$class_name</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// The last segment of the class name matches the file name.</span>
    <span class="nv">$class</span> <span class="o">=</span> <span class="nb">substr</span><span class="p">(</span><span class="nv">$class_name</span><span class="p">,</span> <span class="nb">strrpos</span><span class="p">(</span><span class="nv">$class_name</span><span class="p">,</span> <span class="s2">&quot;</span><span class="se">\\</span><span class="s2">&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
    <span class="c1">// `flatbuffers` root.</span>
    <span class="nv">$root_dir</span> <span class="o">=</span> <span class="nb">join</span><span class="p">(</span><span class="nx">DIRECTORY_SEPARATOR</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="nb">dirname</span><span class="p">(</span><span class="nb">dirname</span><span class="p">(</span><span class="no">__FILE__</span><span class="p">))));</span> 

    <span class="c1">// Contains the `*.php` files for the FlatBuffers library and the `flatc`</span>
    <span class="c1">// generated files.</span>
    <span class="nv">$paths</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="nb">join</span><span class="p">(</span><span class="nx">DIRECTORY_SEPARATOR</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="nv">$root_dir</span><span class="p">,</span> <span class="s2">&quot;php&quot;</span><span class="p">)),</span>
      <span class="nb">join</span><span class="p">(</span><span class="nx">DIRECTORY_SEPARATOR</span><span class="p">,</span> 
        <span class="k">array</span><span class="p">(</span><span class="nv">$root_dir</span><span class="p">,</span> <span class="s2">&quot;samples&quot;</span><span class="p">,</span> <span class="s2">&quot;MyGame&quot;</span><span class="p">,</span> <span class="s2">&quot;Sample&quot;</span><span class="p">)));</span>
    <span class="k">foreach</span> <span class="p">(</span><span class="nv">$paths</span> <span class="k">as</span> <span class="nv">$path</span><span class="p">)</span> <span class="p">{</span>
    <span class="nv">$file</span> <span class="o">=</span> <span class="nb">join</span><span class="p">(</span><span class="nx">DIRECTORY_SEPARATOR</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="nv">$path</span><span class="p">,</span> <span class="nv">$class</span> <span class="o">.</span> <span class="s2">&quot;.php&quot;</span><span class="p">));</span>
    <span class="k">if</span> <span class="p">(</span><span class="nb">file_exists</span><span class="p">(</span><span class="nv">$file</span><span class="p">))</span> <span class="p">{</span>
        <span class="k">require</span><span class="p">(</span><span class="nv">$file</span><span class="p">);</span>
        <span class="k">break</span><span class="p">;</span>
    <span class="p">}</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">flatbuffers</span>

<span class="c1"># Generated by `flatc`.</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">MyGame.Sample.Color</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">MyGame.Sample.Equipment</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">MyGame.Sample.Monster</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">MyGame.Sample.Vec3</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">MyGame.Sample.Weapon</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// import the flatbuffers runtime library</span>
<span class="k">extern</span><span class="w"> </span><span class="k">crate</span><span class="w"> </span><span class="n">flatbuffers</span><span class="p">;</span>

<span class="c1">// import the generated code</span>
<span class="cp">#[allow(dead_code, unused_imports)]</span>
<span class="cp">#[path = </span><span class="s">&quot;./monster_generated.rs&quot;</span><span class="cp">]</span>
<span class="k">mod</span><span class="w"> </span><span class="nn">monster_generated</span><span class="p">;</span>
<span class="k">pub</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">monster_generated</span><span class="p">::</span><span class="n">my_game</span><span class="p">::</span><span class="n">sample</span><span class="p">::{</span><span class="n">root_as_monster</span><span class="p">,</span>
<span class="w">                                            </span><span class="n">Color</span><span class="p">,</span><span class="w"> </span><span class="n">Equipment</span><span class="p">,</span>
<span class="w">                                            </span><span class="n">Monster</span><span class="p">,</span><span class="w"> </span><span class="n">MonsterArgs</span><span class="p">,</span>
<span class="w">                                            </span><span class="n">Vec3</span><span class="p">,</span>
<span class="w">                                            </span><span class="n">Weapon</span><span class="p">,</span><span class="w"> </span><span class="n">WeaponArgs</span><span class="p">};</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="cm">/**</span>
<span class="cm">// make sure that monster_generated.swift is included in your project</span>
<span class="cm">*/</span>
<span class="kd">import</span><span class="w"> </span><span class="nc">Flatbuffers</span>

<span class="c1">// typealiases for convenience</span>
<span class="kd">typealias</span><span class="w"> </span><span class="n">Monster</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MyGame1_Sample_Monster</span>
<span class="kd">typealias</span><span class="w"> </span><span class="n">Weapon</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MyGame1_Sample_Weapon</span>
<span class="kd">typealias</span><span class="w"> </span><span class="n">Color</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MyGame1_Sample_Color</span>
<span class="kd">typealias</span><span class="w"> </span><span class="n">Vec3</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MyGame1_Sample_Vec3</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// note: import flatbuffers with your desired import method</span>

<span class="k">import</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">MyGame</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="kr">from</span><span class="w"> </span><span class="s1">&#39;./monster_generated&#39;</span><span class="p">;</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>对于某些编程语言，运行时库（runtime
                libraries）只是一些代码文件，您需要将其编译进您的应用程序。而其他语言则通过其包管理器提供打包好的库。</p>
              <p>生成的文件包含了用于序列化和反序列化 FlatBuffers（二进制缓冲区）的
                API。因此，这些步骤对于数据的生产者和消费者来说是完全相同的。</p>
              <h2 id="serialization">Serialization（序列化）</h2>
              <p>当所有文件都被包含进您的应用程序后，就可以开始序列化（serialization）数据了！</p>
              <p>使用 FlatBuffers（FlatBuffers
                二进制缓冲区）时，序列化过程可能会显得有些繁琐，因为每一部分数据都必须单独并按照特定顺序（深度优先，先序遍历）进行序列化。这种繁琐性带来了高效的序列化过程，无需堆内存分配，但代价是序列化
                API 更加复杂。</p>
              <p>例如，任何引用类型（如
                <code>table</code>、<code>vector</code>、<code>string</code>）都必须在被其他结构引用之前完成序列化。因此，通常的做法是从叶子节点到根节点依次序列化数据，下面将进行演示。</p>
              <h3 id="flatbufferbuilder">FlatBufferBuilder（FlatBufferBuilder
                构建器）</h3>
              <p>大多数编程语言都使用 Builder（构建器）对象来管理用于序列化数据的二进制数组。它提供了用于序列化数据的
                API，并维护一些内部状态。生成的代码会对 Builder 对象的方法进行封装，从而提供与 schema（模式文件）相适配的
                API。</p>
              <p>首先需要实例化一个 Builder（或复用已有的 Builder），并为其分配一定的内存。当需要时，Builder
                会自动调整底层缓冲区的大小。</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="4:15"><input
                  checked="checked" id="c_6" name="__tabbed_4"
                  type="radio" /><input id="c_7" name="__tabbed_4"
                  type="radio" /><input id="c_8" name="__tabbed_4"
                  type="radio" /><input id="dart_2" name="__tabbed_4"
                  type="radio" /><input id="go_2" name="__tabbed_4"
                  type="radio" /><input id="java_2" name="__tabbed_4"
                  type="radio" /><input id="javascript_2" name="__tabbed_4"
                  type="radio" /><input id="kotlin_2" name="__tabbed_4"
                  type="radio" /><input id="lobster_2" name="__tabbed_4"
                  type="radio" /><input id="lua_2" name="__tabbed_4"
                  type="radio" /><input id="php_2" name="__tabbed_4"
                  type="radio" /><input id="python_2" name="__tabbed_4"
                  type="radio" /><input id="rust_2" name="__tabbed_4"
                  type="radio" /><input id="swift_2" name="__tabbed_4"
                  type="radio" /><input id="typescript_2" name="__tabbed_4"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_6">C++</label><label for="c_7">C</label><label
                    for="c_8">C#</label><label for="dart_2">Dart</label><label
                    for="go_2">Go</label><label for="java_2">Java</label><label
                    for="javascript_2">JavaScript</label><label
                    for="kotlin_2">Kotlin</label><label
                    for="lobster_2">Lobster</label><label
                    for="lua_2">Lua</label><label for="php_2">PHP</label><label
                    for="python_2">Python</label><label
                    for="rust_2">Rust</label><label
                    for="swift_2">Swift</label><label
                    for="typescript_2">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="n">flatbuffers</span><span class="o">::</span><span class="n">FlatBufferBuilder</span><span class="w"> </span><span class="nf">builder</span><span class="p">(</span><span class="mi">1024</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">flatcc_builder_t</span><span class="w"> </span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">B</span><span class="p">;</span>
<span class="n">B</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">builder</span><span class="p">;</span>
<span class="c1">// Initialize the builder object.</span>
<span class="n">flatcc_builder_init</span><span class="p">(</span><span class="n">B</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="n">FlatBufferBuilder</span><span class="w"> </span><span class="n">builder</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">FlatBufferBuilder</span><span class="p">(</span><span class="mi">1024</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="kd">var</span><span class="w"> </span><span class="n">builder</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">fb</span><span class="p">.</span><span class="n">Builder</span><span class="p">(</span><span class="nl">initialSize:</span><span class="w"> </span><span class="m">1024</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="nx">builder</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">flatbuffers</span><span class="p">.</span><span class="nx">NewBuilder</span><span class="p">(</span><span class="mi">1024</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="n">FlatBufferBuilder</span><span class="w"> </span><span class="n">builder</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">FlatBufferBuilder</span><span class="p">(</span><span class="mi">1024</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">builder</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">flatbuffers</span><span class="p">.</span><span class="nx">Builder</span><span class="p">(</span><span class="mf">1024</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">builder</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FlatBufferBuilder</span><span class="p">(</span><span class="m">1024</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。 
let builder = flatbuffers_builder {}
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">-- 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">builder</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">flatbuffers</span><span class="p">.</span><span class="nf">Builder</span><span class="p">(</span><span class="mi">1024</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="nv">$builder</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Google\FlatBuffers\FlatbufferBuilder</span><span class="p">(</span><span class="mi">1024</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1"># 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="n">builder</span> <span class="o">=</span> <span class="n">flatbuffers</span><span class="o">.</span><span class="n">Builder</span><span class="p">(</span><span class="mi">1024</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">builder</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flatbuffers</span><span class="p">::</span><span class="n">FlatBufferBuilder</span><span class="p">::</span><span class="n">with_capacity</span><span class="p">(</span><span class="mi">1024</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">builder</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">FlatBufferBuilder</span><span class="p">(</span><span class="n">initialSize</span><span class="p">:</span><span class="w"> </span><span class="mi">1024</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用一个 1024 字节的底层数组来构造一个 Builder（构建器）对象。</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">builder</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">flatbuffers</span><span class="p">.</span><span class="nx">Builder</span><span class="p">(</span><span class="mf">1024</span><span class="p">);</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>当 Builder（构建器）对象可用后，可以通过 Builder 的 API 以及生成的代码将数据序列化到其中。</p>
              <h3 id="serializing-data">Serializing Data（序列化数据）</h3>
              <p>在本教程中，我们将为一款电脑游戏构建 <code>Monsters</code>（怪物）和
                <code>Weapons</code>（武器）。<code>Weapon</code>（武器）由一个 flatbuffer
                <code>table</code>（表）表示，包含若干字段。其中一个字段是 <code>name</code>（名称），类型为
                <code>string</code>（字符串）；另一个字段是
                <code>damage</code>（伤害），是一个数值型标量。</p>
              <div class="highlight"><table class="highlighttable"><tr><th
                      colspan="2" class="filename"><span
                        class="filename">monster.fbs</span></th></tr><tr><td
                      class="linenos"><div
                        class="linenodiv"><pre><span></span><span class="normal">28</span>
<span class="normal">29</span>
<span class="normal">30</span>
<span class="normal">31</span></pre></div></td><td
                      class="code"><div><pre><span></span><code><span class="n">table</span><span class="w"> </span><span class="n">Weapon</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="nl">name</span><span class="p">:</span><span class="n">string</span><span class="p">;</span>
<span class="w">  </span><span class="nl">damage</span><span class="p">:</span><span class="kt">short</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></td></tr></table></div>
              <h4 id="strings">Strings（字符串）</h4>
              <p>由于 <code>string</code>（字符串）是引用类型，因此在将其赋值给
                <code>Weapon</code>（武器）表的
                <code>name</code>（名称）字段之前，需要先对其进行序列化。这可以通过 Builder（构建器）对象的
                <code>CreateString</code> 方法完成。</p>
              <p>下面我们来序列化两个武器名称字符串。</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="5:15"><input
                  checked="checked" id="c_9" name="__tabbed_5"
                  type="radio" /><input id="c_10" name="__tabbed_5"
                  type="radio" /><input id="c_11" name="__tabbed_5"
                  type="radio" /><input id="dart_3" name="__tabbed_5"
                  type="radio" /><input id="go_3" name="__tabbed_5"
                  type="radio" /><input id="java_3" name="__tabbed_5"
                  type="radio" /><input id="javascript_3" name="__tabbed_5"
                  type="radio" /><input id="kotlin_3" name="__tabbed_5"
                  type="radio" /><input id="lobster_3" name="__tabbed_5"
                  type="radio" /><input id="lua_3" name="__tabbed_5"
                  type="radio" /><input id="php_3" name="__tabbed_5"
                  type="radio" /><input id="python_3" name="__tabbed_5"
                  type="radio" /><input id="rust_3" name="__tabbed_5"
                  type="radio" /><input id="swift_3" name="__tabbed_5"
                  type="radio" /><input id="typescript_3" name="__tabbed_5"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_9">C++</label><label for="c_10">C</label><label
                    for="c_11">C#</label><label for="dart_3">Dart</label><label
                    for="go_3">Go</label><label for="java_3">Java</label><label
                    for="javascript_3">JavaScript</label><label
                    for="kotlin_3">Kotlin</label><label
                    for="lobster_3">Lobster</label><label
                    for="lua_3">Lua</label><label for="php_3">PHP</label><label
                    for="python_3">Python</label><label
                    for="rust_3">Rust</label><label
                    for="swift_3">Swift</label><label
                    for="typescript_3">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">flatbuffers</span><span class="o">::</span><span class="n">Offset</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span><span class="w"> </span><span class="n">weapon_one_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">CreateString</span><span class="p">(</span><span class="s">&quot;Sword&quot;</span><span class="p">);</span>
<span class="n">flatbuffers</span><span class="o">::</span><span class="n">Offset</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span><span class="w"> </span><span class="n">weapon_two_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">CreateString</span><span class="p">(</span><span class="s">&quot;Axe&quot;</span><span class="p">);</span>
</code></pre></div>
                    <p><code>flatbuffers::Offset&lt;&gt;</code>
                      只是一个与特定类型关联的“带类型”整数。它有助于使数值偏移量具有更强的类型安全性。</p>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">flatbuffers_string_ref_t</span><span class="w"> </span><span class="n">weapon_one_name</span><span class="w"> </span>
<span class="w">    </span><span class="o">=</span><span class="w"> </span><span class="n">flatbuffers_string_create_str</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Sword&quot;</span><span class="p">);</span>
<span class="n">flatbuffers_string_ref_t</span><span class="w"> </span><span class="n">weapon_two_name</span><span class="w"> </span>
<span class="w">    </span><span class="o">=</span><span class="w"> </span><span class="n">flatbuffers_string_create_str</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Axe&quot;</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">Offset</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span><span class="w"> </span><span class="n">weaponOneName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">CreateString</span><span class="p">(</span><span class="s">&quot;Sword&quot;</span><span class="p">);</span>
<span class="n">Offset</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span><span class="w"> </span><span class="n">weaponTwoName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">CreateString</span><span class="p">(</span><span class="s">&quot;Axe&quot;</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">weaponOneName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">writeString</span><span class="p">(</span><span class="s2">&quot;Sword&quot;</span><span class="p">);</span>
<span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">weaponTwoName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">writeString</span><span class="p">(</span><span class="s2">&quot;Axe&quot;</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nx">weaponOne</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">CreateString</span><span class="p">(</span><span class="s">&quot;Sword&quot;</span><span class="p">)</span>
<span class="nx">weaponTwo</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">CreateString</span><span class="p">(</span><span class="s">&quot;Axe&quot;</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">int</span><span class="w"> </span><span class="n">weaponOneName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="na">createString</span><span class="p">(</span><span class="s">&quot;Sword&quot;</span><span class="p">)</span>
<span class="kt">int</span><span class="w"> </span><span class="n">weaponTwoName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="na">createString</span><span class="p">(</span><span class="s">&quot;Axe&quot;</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">weaponOne</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">createString</span><span class="p">(</span><span class="s1">&#39;Sword&#39;</span><span class="p">);</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">weaponTwo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">createString</span><span class="p">(</span><span class="s1">&#39;Axe&#39;</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">val</span><span class="w"> </span><span class="nv">weaponOneName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="na">createString</span><span class="p">(</span><span class="s">&quot;Sword&quot;</span><span class="p">)</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">weaponTwoName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="na">createString</span><span class="p">(</span><span class="s">&quot;Axe&quot;</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>let weapon_one = builder.CreateString(&quot;Sword&quot;)
let weapon_two = builder.CreateString(&quot;Axe&quot;)
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">local</span><span class="w"> </span><span class="nv">weaponOne</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">builder</span><span class="p">:</span><span class="nf">CreateString</span><span class="p">(</span><span class="s2">&quot;Sword&quot;</span><span class="p">)</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">weaponTwo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">builder</span><span class="p">:</span><span class="nf">CreateString</span><span class="p">(</span><span class="s2">&quot;Axe&quot;</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nv">$weapon_one_name</span> <span class="o">=</span> <span class="nv">$builder</span><span class="o">-&gt;</span><span class="na">createString</span><span class="p">(</span><span class="s2">&quot;Sword&quot;</span><span class="p">)</span>
<span class="nv">$weapon_two_name</span> <span class="o">=</span> <span class="nv">$builder</span><span class="o">-&gt;</span><span class="na">createString</span><span class="p">(</span><span class="s2">&quot;Axe&quot;</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">weapon_one</span> <span class="o">=</span> <span class="n">builder</span><span class="o">.</span><span class="n">CreateString</span><span class="p">(</span><span class="s1">&#39;Sword&#39;</span><span class="p">)</span>
<span class="n">weapon_two</span> <span class="o">=</span> <span class="n">builder</span><span class="o">.</span><span class="n">CreateString</span><span class="p">(</span><span class="s1">&#39;Axe&#39;</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">weapon_one_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">create_string</span><span class="p">(</span><span class="s">&quot;Sword&quot;</span><span class="p">);</span>
<span class="kd">let</span><span class="w"> </span><span class="n">weapon_two_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">create_string</span><span class="p">(</span><span class="s">&quot;Axe&quot;</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="nv">weapon1Name</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">create</span><span class="p">(</span><span class="n">string</span><span class="p">:</span><span class="w"> </span><span class="s">&quot;Sword&quot;</span><span class="p">)</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">weapon2Name</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">create</span><span class="p">(</span><span class="n">string</span><span class="p">:</span><span class="w"> </span><span class="s">&quot;Axe&quot;</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="nx">weaponOne</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">createString</span><span class="p">(</span><span class="s1">&#39;Sword&#39;</span><span class="p">);</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">weaponTwo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">createString</span><span class="p">(</span><span class="s1">&#39;Axe&#39;</span><span class="p">);</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>这一步执行了实际的序列化操作（字符串数据被复制到底层数组中），并返回一个 offset（偏移量）。可以将 offset
                理解为该引用的句柄。它只是一个“带类型”的数值偏移，指向该数据在缓冲区中的位置。</p>
              <h4 id="tables">Tables（表）</h4>
              <p>现在我们已经序列化了一些名称，可以开始序列化 <code>Weapon</code>（武器）表了。在这里，我们将使用由
                <code>flatc</code> 生成的辅助函数之一。<code>CreateWeapon</code> 函数接收
                Builder（构建器）对象、武器名称的 offset（偏移量）以及伤害字段的数值作为参数。</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="6:15"><input
                  checked="checked" id="c_12" name="__tabbed_6"
                  type="radio" /><input id="c_13" name="__tabbed_6"
                  type="radio" /><input id="c_14" name="__tabbed_6"
                  type="radio" /><input id="dart_4" name="__tabbed_6"
                  type="radio" /><input id="go_4" name="__tabbed_6"
                  type="radio" /><input id="java_4" name="__tabbed_6"
                  type="radio" /><input id="javascript_4" name="__tabbed_6"
                  type="radio" /><input id="kotlin_4" name="__tabbed_6"
                  type="radio" /><input id="lobster_4" name="__tabbed_6"
                  type="radio" /><input id="lua_4" name="__tabbed_6"
                  type="radio" /><input id="php_4" name="__tabbed_6"
                  type="radio" /><input id="python_4" name="__tabbed_6"
                  type="radio" /><input id="rust_4" name="__tabbed_6"
                  type="radio" /><input id="swift_4" name="__tabbed_6"
                  type="radio" /><input id="typescript_4" name="__tabbed_6"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_12">C++</label><label for="c_13">C</label><label
                    for="c_14">C#</label><label for="dart_4">Dart</label><label
                    for="go_4">Go</label><label for="java_4">Java</label><label
                    for="javascript_4">JavaScript</label><label
                    for="kotlin_4">Kotlin</label><label
                    for="lobster_4">Lobster</label><label
                    for="lua_4">Lua</label><label for="php_4">PHP</label><label
                    for="python_4">Python</label><label
                    for="rust_4">Rust</label><label
                    for="swift_4">Swift</label><label
                    for="typescript_4">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">short</span><span class="w"> </span><span class="n">weapon_one_damage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span>
<span class="kt">short</span><span class="w"> </span><span class="n">weapon_two_damage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span>

<span class="c1">// 使用 `CreateWeapon()` 快捷方法创建包含所有字段的武器对象</span>
<span class="c1">// 并设置所有字段。</span>
<span class="n">flatbuffers</span><span class="o">::</span><span class="n">Offset</span><span class="o">&lt;</span><span class="n">Weapon</span><span class="o">&gt;</span><span class="w"> </span><span class="n">sword</span><span class="w"> </span><span class="o">=</span>
<span class="w">    </span><span class="n">CreateWeapon</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">weapon_one_name</span><span class="p">,</span><span class="w"> </span><span class="n">weapon_one_damage</span><span class="p">);</span>
<span class="n">flatbuffers</span><span class="o">::</span><span class="n">Offset</span><span class="o">&lt;</span><span class="n">Weapon</span><span class="o">&gt;</span><span class="w"> </span><span class="n">axe</span><span class="w"> </span><span class="o">=</span>
<span class="w">    </span><span class="n">CreateWeapon</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">weapon_two_name</span><span class="p">,</span><span class="w"> </span><span class="n">weapon_two_damage</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">uint16_t</span><span class="w"> </span><span class="n">weapon_one_damage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span>
<span class="kt">uint16_t</span><span class="w"> </span><span class="n">weapon_two_damage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span>

<span class="n">ns</span><span class="p">(</span><span class="n">Weapon_ref_t</span><span class="p">)</span><span class="w"> </span><span class="n">sword</span><span class="w"> </span>
<span class="w">    </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Weapon_create</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">weapon_one_name</span><span class="p">,</span><span class="w"> </span><span class="n">weapon_one_damage</span><span class="p">));</span>
<span class="n">ns</span><span class="p">(</span><span class="n">Weapon_ref_t</span><span class="p">)</span><span class="w"> </span><span class="n">axe</span><span class="w"> </span>
<span class="w">    </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Weapon_create</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">weapon_two_name</span><span class="p">,</span><span class="w"> </span><span class="n">weapon_two_damage</span><span class="p">));</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">short</span><span class="w"> </span><span class="n">weaponOneDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span>
<span class="kt">short</span><span class="w"> </span><span class="n">weaponTwoDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span>

<span class="c1">// 使用 `CreateWeapon()` 辅助函数来创建武器对象，因为我们设置了每个字段。</span>
<span class="n">Offset</span><span class="o">&lt;</span><span class="n">Weapon</span><span class="o">&gt;</span><span class="w"> </span><span class="n">sword</span><span class="w"> </span><span class="o">=</span>
<span class="w">    </span><span class="n">Weapon</span><span class="p">.</span><span class="n">CreateWeapon</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">weaponOneName</span><span class="p">,</span><span class="w"> </span><span class="n">weaponOneDamage</span><span class="p">);</span>
<span class="n">Offset</span><span class="o">&lt;</span><span class="n">Weapon</span><span class="o">&gt;</span><span class="w"> </span><span class="n">axe</span><span class="w"> </span><span class="o">=</span>
<span class="w">    </span><span class="n">Weapon</span><span class="p">.</span><span class="n">CreateWeapon</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">weaponTwoName</span><span class="p">,</span><span class="w"> </span><span class="n">weaponTwoDamage</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">weaponOneDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span><span class="p">;</span>
<span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">weaponTwoDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">;</span>

<span class="kd">final</span><span class="w"> </span><span class="n">swordBuilder</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">myGame</span><span class="p">.</span><span class="n">WeaponBuilder</span><span class="p">(</span><span class="n">builder</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">begin</span><span class="p">()</span>
<span class="w">    </span><span class="p">..</span><span class="n">addNameOffset</span><span class="p">(</span><span class="n">weaponOneName</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">addDamage</span><span class="p">(</span><span class="n">weaponOneDamage</span><span class="p">);</span>
<span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">sword</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">swordBuilder</span><span class="p">.</span><span class="n">finish</span><span class="p">();</span>

<span class="kd">final</span><span class="w"> </span><span class="n">axeBuilder</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">myGame</span><span class="p">.</span><span class="n">WeaponBuilder</span><span class="p">(</span><span class="n">builder</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">begin</span><span class="p">()</span>
<span class="w">    </span><span class="p">..</span><span class="n">addNameOffset</span><span class="p">(</span><span class="n">weaponTwoName</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">addDamage</span><span class="p">(</span><span class="n">weaponTwoDamage</span><span class="p">);</span>
<span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">axe</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">axeBuilder</span><span class="p">.</span><span class="n">finish</span><span class="p">();</span>
</code></pre></div>
                    <p>注意，作为另一种选择，可以使用生成式 Builder（构建器）类将前面的步骤合并起来。</p>
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">final</span><span class="w"> </span><span class="n">myGame</span><span class="p">.</span><span class="n">WeaponBuilder</span><span class="w"> </span><span class="n">sword</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">myGame</span><span class="p">.</span><span class="n">WeaponObjectBuilder</span><span class="p">(</span>
<span class="w">    </span><span class="nl">name:</span><span class="w"> </span><span class="s2">&quot;Sword&quot;</span><span class="p">,</span>
<span class="w">    </span><span class="nl">damage:</span><span class="w"> </span><span class="m">3</span><span class="p">,</span>
<span class="p">);</span>

<span class="kd">final</span><span class="w"> </span><span class="n">myGame</span><span class="p">.</span><span class="n">WeaponBuilder</span><span class="w"> </span><span class="n">axe</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">myGame</span><span class="p">.</span><span class="n">WeaponObjectBuilder</span><span class="p">(</span>
<span class="w">    </span><span class="nl">name:</span><span class="w"> </span><span class="s2">&quot;Axe&quot;</span><span class="p">,</span>
<span class="w">    </span><span class="nl">damage:</span><span class="w"> </span><span class="m">5</span><span class="p">,</span>
<span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 创建第一个 `Weapon`（“Sword”）。</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">WeaponStart</span><span class="p">(</span><span class="nx">builder</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">WeaponAddName</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">weaponOne</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">WeaponAddDamage</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span>
<span class="nx">sword</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">sample</span><span class="p">.</span><span class="nx">WeaponEnd</span><span class="p">(</span><span class="nx">builder</span><span class="p">)</span>

<span class="c1">// 创建第二个 `Weapon`（“Axe”）。</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">WeaponStart</span><span class="p">(</span><span class="nx">builder</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">WeaponAddName</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">weaponTwo</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">WeaponAddDamage</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span>
<span class="nx">axe</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">sample</span><span class="p">.</span><span class="nx">WeaponEnd</span><span class="p">(</span><span class="nx">builder</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">short</span><span class="w"> </span><span class="n">weaponOneDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span>
<span class="kt">short</span><span class="w"> </span><span class="n">weaponTwoDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span>

<span class="c1">// 使用 `createWeapon()` 辅助函数来创建武器对象，因为我们设置了每个字段。</span>
<span class="kt">int</span><span class="w"> </span><span class="n">sword</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Weapon</span><span class="p">.</span><span class="na">createWeapon</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">weaponOneName</span><span class="p">,</span><span class="w"> </span><span class="n">weaponOneDamage</span><span class="p">);</span>
<span class="kt">int</span><span class="w"> </span><span class="n">axe</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Weapon</span><span class="p">.</span><span class="na">createWeapon</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">weaponTwoName</span><span class="p">,</span><span class="w"> </span><span class="n">weaponTwoDamage</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 创建第一个 `Weapon`（“Sword”）。</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">startWeapon</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">addName</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">weaponOne</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">addDamage</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">);</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">sword</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">endWeapon</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>

<span class="c1">// 创建第二个 `Weapon`（“Axe”）。</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">startWeapon</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">addName</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">weaponTwo</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">addDamage</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">);</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">axe</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">endWeapon</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">val</span><span class="w"> </span><span class="nv">weaponOneDamage</span><span class="p">:</span><span class="w"> </span><span class="kt">Short</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span><span class="p">;</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">weaponTwoDamage</span><span class="p">:</span><span class="w"> </span><span class="kt">Short</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">;</span>

<span class="c1">// 使用 `createWeapon()` 辅助函数来创建武器对象，因为我们设置了每个字段。</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">sword</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Weapon</span><span class="p">.</span><span class="na">createWeapon</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">weaponOneName</span><span class="p">,</span><span class="w"> </span><span class="n">weaponOneDamage</span><span class="p">)</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">axe</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Weapon</span><span class="p">.</span><span class="na">createWeapon</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">weaponTwoName</span><span class="p">,</span><span class="w"> </span><span class="n">weaponTwoDamage</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>let sword = MyGame_Sample_WeaponBuilder { b }
      .start()
      .add_name(weapon_one)
      .add_damage(3)
      .end()

let axe = MyGame_Sample_WeaponBuilder { b }
      .start()
      .add_name(weapon_two)
      .add_damage(5)
      .end()
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code></code><span class="c1">-- 创建第一个 'Weapon'（“Sword”）</span>
<span class="nv">weapon</span><span class="p">.</span><span class="nf">Start</span><span class="p">(</span><span class="nv">builder</span><span class="p">)</span>
<span class="nv">weapon</span><span class="p">.</span><span class="nf">AddName</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="nv">weaponOne</span><span class="p">)</span>
<span class="nv">weapon</span><span class="p">.</span><span class="nf">AddDamage</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">sword</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">weapon</span><span class="p">.</span><span class="nf">End</span><span class="p">(</span><span class="nv">builder</span><span class="p">)</span>

<span class="c1">-- 创建第二个 'Weapon'（“Axe”）</span>
<span class="nv">weapon</span><span class="p">.</span><span class="nf">Start</span><span class="p">(</span><span class="nv">builder</span><span class="p">)</span>
<span class="nv">weapon</span><span class="p">.</span><span class="nf">AddName</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="nv">weaponTwo</span><span class="p">)</span>
<span class="nv">weapon</span><span class="p">.</span><span class="nf">AddDamage</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">axe</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">weapon</span><span class="p">.</span><span class="nf">End</span><span class="p">(</span><span class="nv">builder</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nv">$sword</span> <span class="o">=</span> <span class="nx">\MyGame\Sample\Weapon</span><span class="o">::</span><span class="na">CreateWeapon</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="nv">$weapon_one_name</span><span class="p">,</span> <span class="mi">3</span><span class="p">);</span>
<span class="nv">$axe</span> <span class="o">=</span> <span class="nx">\MyGame\Sample\Weapon</span><span class="o">::</span><span class="na">CreateWeapon</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="nv">$weapon_two_name</span><span class="p">,</span> <span class="mi">5</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1"># 创建第一个 `Weapon`（“Sword”）。</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Weapon</span><span class="o">.</span><span class="n">Start</span><span class="p">(</span><span class="n">builder</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Weapon</span><span class="o">.</span><span class="n">AddName</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="n">weapon_one</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Weapon</span><span class="o">.</span><span class="n">AddDamage</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">sword</span> <span class="o">=</span> <span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Weapon</span><span class="o">.</span><span class="n">End</span><span class="p">(</span><span class="n">builder</span><span class="p">)</span>

<span class="c1"># 创建第二个 `Weapon`（“Axe”）。</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Weapon</span><span class="o">.</span><span class="n">Start</span><span class="p">(</span><span class="n">builder</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Weapon</span><span class="o">.</span><span class="n">AddName</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="n">weapon_two</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Weapon</span><span class="o">.</span><span class="n">AddDamage</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="n">axe</span> <span class="o">=</span> <span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Weapon</span><span class="o">.</span><span class="n">End</span><span class="p">(</span><span class="n">builder</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 使用 `Weapon::create` 快捷方法，通过命名字段参数创建武器对象。</span>
<span class="c1">// arguments.</span>
<span class="kd">let</span><span class="w"> </span><span class="n">sword</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Weapon</span><span class="p">::</span><span class="n">create</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">WeaponArgs</span><span class="p">{</span>
<span class="w">    </span><span class="n">name</span><span class="p">:</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">weapon_one_name</span><span class="p">),</span>
<span class="w">    </span><span class="n">damage</span><span class="p">:</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span>
<span class="p">});</span>
<span class="kd">let</span><span class="w"> </span><span class="n">axe</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Weapon</span><span class="p">::</span><span class="n">create</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">WeaponArgs</span><span class="p">{</span>
<span class="w">    </span><span class="n">name</span><span class="p">:</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">weapon_two_name</span><span class="p">),</span>
<span class="w">    </span><span class="n">damage</span><span class="p">:</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span>
<span class="p">});</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 通过调用 startWeapon 开始创建武器对象</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">weapon1Start</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Weapon</span><span class="p">.</span><span class="n">startWeapon</span><span class="p">(&amp;</span><span class="n">builder</span><span class="p">)</span>
<span class="n">Weapon</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">name</span><span class="p">:</span><span class="w"> </span><span class="n">weapon1Name</span><span class="p">,</span><span class="w"> </span><span class="p">&amp;</span><span class="n">builder</span><span class="p">)</span>
<span class="n">Weapon</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">damage</span><span class="p">:</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="p">&amp;</span><span class="n">builder</span><span class="p">)</span>
<span class="c1">// 通过传递武器1的起始点来结束该对象的创建</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">sword</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Weapon</span><span class="p">.</span><span class="n">endWeapon</span><span class="p">(&amp;</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">start</span><span class="p">:</span><span class="w"> </span><span class="n">weapon1Start</span><span class="p">)</span>

<span class="kd">let</span><span class="w"> </span><span class="nv">weapon2Start</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Weapon</span><span class="p">.</span><span class="n">startWeapon</span><span class="p">(&amp;</span><span class="n">builder</span><span class="p">)</span>
<span class="n">Weapon</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">name</span><span class="p">:</span><span class="w"> </span><span class="n">weapon2Name</span><span class="p">,</span><span class="w"> </span><span class="p">&amp;</span><span class="n">builder</span><span class="p">)</span>
<span class="n">Weapon</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">damage</span><span class="p">:</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="p">&amp;</span><span class="n">builder</span><span class="p">)</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">axe</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Weapon</span><span class="p">.</span><span class="n">endWeapon</span><span class="p">(&amp;</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">start</span><span class="p">:</span><span class="w"> </span><span class="n">weapon2Start</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 创建第一个 `Weapon`（“Sword”）。</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">startWeapon</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">addName</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">weaponOne</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">addDamage</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">);</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">sword</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">endWeapon</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>

<span class="c1">// 创建第二个 `Weapon`（“Axe”）。</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">startWeapon</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">addName</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">weaponTwo</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">addDamage</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">);</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">axe</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">.</span><span class="nx">endWeapon</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>由 <code>flatc</code> 生成的函数（如 <code>CreateWeapon</code>）只是由各种
                Builder（构建器）API 方法组合而成。因此，并不是必须使用生成的代码，但这样可以让操作更加简单和紧凑。</p>
              <p>与 <code>CreateString</code> 方法类似，表的序列化函数会返回序列化后
                <code>Weapon</code>（武器）表在缓冲区中的 offset（偏移量）。</p>
              <p>现在我们已经序列化了一些 <code>Weapons</code>（武器），接下来可以序列化一个
                <code>Monster</code>（怪物）。再次查看
                schema（模式文件），可以发现该表包含更多不同类型的字段。其中有些字段需要提前进行序列化，原因与我们在序列化武器表之前先序列化名称字符串相同。</p>
              <div class="admonition note inline end">
                <p class="admonition-title">Note（注意）</p>
                <p>对于表字段的序列化顺序，并没有强制要求，您可以按照任意顺序进行序列化。如果不对某个字段进行序列化，则该字段会被赋予
                  <code>null</code>（空值），这可以通过使用 0 作为 offset（偏移量）值来实现。</p>
              </div>
              <div class="highlight"><table class="highlighttable"><tr><th
                      colspan="2" class="filename"><span
                        class="filename">monster.fbs</span></th></tr><tr><td
                      class="linenos"><div
                        class="linenodiv"><pre><span></span><span class="normal">15</span>
<span class="normal">16</span>
<span class="normal">17</span>
<span class="normal">18</span>
<span class="normal">19</span>
<span class="normal">20</span>
<span class="normal">21</span>
<span class="normal">22</span>
<span class="normal">23</span>
<span class="normal">24</span>
<span class="normal">25</span>
<span class="normal">26</span></pre></div></td><td
                      class="code"><div><pre><span></span><code><span class="n">table</span><span class="w"> </span><span class="n">Monster</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="nl">pos</span><span class="p">:</span><span class="n">Vec3</span><span class="p">;</span>
<span class="w">  </span><span class="nl">mana</span><span class="p">:</span><span class="kt">short</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">150</span><span class="p">;</span>
<span class="w">  </span><span class="nl">hp</span><span class="p">:</span><span class="kt">short</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">100</span><span class="p">;</span>
<span class="w">  </span><span class="nl">name</span><span class="p">:</span><span class="n">string</span><span class="p">;</span>
<span class="w">  </span><span class="nl">friendly</span><span class="p">:</span><span class="kt">bool</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">false</span><span class="w"> </span><span class="p">(</span><span class="n">deprecated</span><span class="p">);</span>
<span class="w">  </span><span class="nl">inventory</span><span class="p">:[</span><span class="n">ubyte</span><span class="p">];</span>
<span class="w">  </span><span class="nl">color</span><span class="p">:</span><span class="n">Color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Blue</span><span class="p">;</span>
<span class="w">  </span><span class="nl">weapons</span><span class="p">:[</span><span class="n">Weapon</span><span class="p">];</span>
<span class="w">  </span><span class="nl">equipped</span><span class="p">:</span><span class="n">Equipment</span><span class="p">;</span>
<span class="w">  </span><span class="nl">path</span><span class="p">:[</span><span class="n">Vec3</span><span class="p">];</span>
<span class="p">}</span>
</code></pre></div></td></tr></table></div>
              <h4 id="vectors">Vectors（向量）</h4>
              <p><code>weapons</code> 字段是一个 <code>vector</code>（向量），其元素为
                <code>Weapon</code>（武器）表。我们已经序列化了两个
                <code>Weapons</code>，现在只需要将这些 offset（偏移量）序列化为一个
                <code>vector</code>。Builder（构建器）对象提供了多种方式来创建
                <code>vectors</code>。</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="7:15"><input
                  checked="checked" id="c_15" name="__tabbed_7"
                  type="radio" /><input id="c_16" name="__tabbed_7"
                  type="radio" /><input id="c_17" name="__tabbed_7"
                  type="radio" /><input id="dart_5" name="__tabbed_7"
                  type="radio" /><input id="go_5" name="__tabbed_7"
                  type="radio" /><input id="java_5" name="__tabbed_7"
                  type="radio" /><input id="javascript_5" name="__tabbed_7"
                  type="radio" /><input id="kotlin_5" name="__tabbed_7"
                  type="radio" /><input id="lobster_5" name="__tabbed_7"
                  type="radio" /><input id="lua_5" name="__tabbed_7"
                  type="radio" /><input id="php_5" name="__tabbed_7"
                  type="radio" /><input id="python_5" name="__tabbed_7"
                  type="radio" /><input id="rust_5" name="__tabbed_7"
                  type="radio" /><input id="swift_5" name="__tabbed_7"
                  type="radio" /><input id="typescript_5" name="__tabbed_7"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_15">C++</label><label for="c_16">C</label><label
                    for="c_17">C#</label><label for="dart_5">Dart</label><label
                    for="go_5">Go</label><label for="java_5">Java</label><label
                    for="javascript_5">JavaScript</label><label
                    for="kotlin_5">Kotlin</label><label
                    for="lobster_5">Lobster</label><label
                    for="lua_5">Lua</label><label for="php_5">PHP</label><label
                    for="python_5">Python</label><label
                    for="rust_5">Rust</label><label
                    for="swift_5">Swift</label><label
                    for="typescript_5">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 创建之前生成的 offset 的 std::vector。</span>
std::vector<flatbuffers::Offset<Weapon>> weapons_vector;
weapons_vector.push_back(sword);
weapons_vector.push_back(axe);

<span class="c1">// 然后将 std::vector 序列化到缓冲区，并再次获得该向量的 Offset。</span>
<span class="c1">// 这里使用 `auto`，因为完整类型较长，本质上只是一个“带类型”的数字。</span>
auto weapons = builder.CreateVector(weapons_vector);
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 我们使用内部 builder 栈来实现动态向量。</span>
ns(Weapon_vec_start(B));
ns(Weapon_vec_push(B, sword));
ns(Weapon_vec_push(B, axe));
ns(Weapon_vec_ref_t) weapons = ns(Weapon_vec_end(B));
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 创建两个武器 offset 的数组。</span>
var weaps = new Offset<Weapon>[2];
weaps[0] = sword;
weaps[1] = axe;

<span class="c1">// 将 `weaps` 数组传递给 `CreateWeaponsVector()` 方法以创建 FlatBuffer 向量。</span>
var weapons = Monster.CreateWeaponsVector(builder, weaps);
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 如果使用 Builder 类，序列化 `[sword,axe]`</span>
final weapons = builder.writeList([sword, axe]);

<span class="c1">// 如果使用 ObjectBuilders，只需从两个 `Weapon` 创建一个数组</span>
final List<myGame.WeaponBuilder> weaps = [sword, axe];
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 创建一个 FlatBuffer 向量并将武器插入。</span>
<span class="c1">// 注意：由于我们是前置插入数据，所以顺序要反过来。</span>
sample.MonsterStartWeaponsVector(builder, 2)
builder.PrependUOffsetT(axe)
builder.PrependUOffsetT(sword)
weapons := builder.EndVector(2)
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 将两个武器放入数组，并传递给 </span>
<span class="c1">// `createWeaponsVector()` 方法以创建 FlatBuffer 向量。</span>
int[] weaps = new int[2];
weaps[0] = sword;
weaps[1] = axe;

<span class="c1">// 将 `weaps` 数组传递给 `createWeaponsVector()` 方法以创建 FlatBuffer 向量。</span>
int weapons = Monster.createWeaponsVector(builder, weaps);
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 从两个 `Weapon` 创建数组并传递给</span>
<span class="c1">// `createWeaponsVector()` 方法以创建 FlatBuffer 向量。</span>
var weaps = [sword, axe];
var weapons = MyGame.Sample.Monster.createWeaponsVector(builder, weaps);
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 将两个武器放入数组，并传递给 </span>
<span class="c1">// `createWeaponsVector()` 方法以创建 FlatBuffer 向量。</span>
val weaps = intArrayOf(sword, axe)

<span class="c1">// 将 `weaps` 数组传递给 `createWeaponsVector()` 方法以创建 FlatBuffer 向量。</span>
val weapons = Monster.createWeaponsVector(builder, weaps)
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>let weapons = builder.MyGame_Sample_MonsterCreateWeaponsVector([sword, axe])
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">-- 创建一个 FlatBuffer 向量并插入武器。</span>
<span class="c1">-- 注意：由于我们是前置插入数据，所以顺序要反过来。</span>
monster.StartWeaponsVector(builder, 2)
builder:PrependUOffsetTRelative(axe)
builder:PrependUOffsetTRelative(sword)
local weapons = builder:EndVector(2)
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 从两个 `Weapon` 创建数组并传递给</span>
<span class="c1">// `CreateWeaponsVector()` 方法以创建 FlatBuffer 向量。</span>
$weaps = array($sword, $axe);
$weapons = \MyGame\Sample\Monster::CreateWeaponsVector($builder, $weaps);
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1"># 创建一个 FlatBuffer 向量并插入武器。</span>
<span class="c1"># 注意：由于我们是前置插入数据，所以顺序要反过来。</span>
MyGame.Sample.Monster.StartWeaponsVector(builder, 2)
builder.PrependUOffsetTRelative(axe)
builder.PrependUOffsetTRelative(sword)
weapons = builder.EndVector()
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 创建一个 FlatBuffer `vector`，包含上面创建的 sword 和 axe 的 offset。</span>
let weapons = builder.create_vector(&[sword, axe]);
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 创建一个 FlatBuffer `vector`，包含上面创建的 sword 和 axe 的 offset。</span>
let weaponsOffset = builder.createVector(ofOffsets: [sword, axe])
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// 从两个 `Weapon` 创建数组并传递给</span>
<span class="c1">// `createWeaponsVector()` 方法以创建 FlatBuffer 向量。</span>
let weaps = [sword, axe];
let weapons = MyGame.Sample.Monster.createWeaponsVector(builder, weaps);
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>同时，我们还可以序列化另外两个向量字段：<code>inventory</code>
                字段只是一个标量向量，<code>path</code>
                字段是结构体的向量（本质上也是标量数据）。因此，这些向量可以更直接地进行序列化。</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="8:15"><input
                  checked="checked" id="c_18" name="__tabbed_8"
                  type="radio" /><input id="c_19" name="__tabbed_8"
                  type="radio" /><input id="c_20" name="__tabbed_8"
                  type="radio" /><input id="dart_6" name="__tabbed_8"
                  type="radio" /><input id="go_6" name="__tabbed_8"
                  type="radio" /><input id="java_6" name="__tabbed_8"
                  type="radio" /><input id="javascript_6" name="__tabbed_8"
                  type="radio" /><input id="kotlin_6" name="__tabbed_8"
                  type="radio" /><input id="lobster_6" name="__tabbed_8"
                  type="radio" /><input id="lua_6" name="__tabbed_8"
                  type="radio" /><input id="php_6" name="__tabbed_8"
                  type="radio" /><input id="python_6" name="__tabbed_8"
                  type="radio" /><input id="rust_6" name="__tabbed_8"
                  type="radio" /><input id="swift_6" name="__tabbed_8"
                  type="radio" /><input id="typescript_6" name="__tabbed_8"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_18">C++</label><label for="c_19">C</label><label
                    for="c_20">C#</label><label for="dart_6">Dart</label><label
                    for="go_6">Go</label><label for="java_6">Java</label><label
                    for="javascript_6">JavaScript</label><label
                    for="kotlin_6">Kotlin</label><label
                    for="lobster_6">Lobster</label><label
                    for="lua_6">Lua</label><label for="php_6">PHP</label><label
                    for="python_6">Python</label><label
                    for="rust_6">Rust</label><label
                    for="swift_6">Swift</label><label
                    for="typescript_6">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create a `vector` representing the inventory of the Orc. Each number（创建一个代表兽人库存的 `vector（向量）`。每个数字）</span>
<span class="c1">// could correspond to an item that can be claimed after he is slain.（都可以对应一个在他被击败后可获得的物品。）</span>
<span class="kt">unsigned</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">treasure</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">};</span>
<span class="n">flatbuffers</span><span class="o">::</span><span class="n">Offset</span><span class="o">&lt;</span><span class="n">flatbuffers</span><span class="o">::</span><span class="n">Vector</span><span class="o">&lt;</span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">char</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">inventory</span><span class="w"> </span><span class="o">=</span>
<span class="w">    </span><span class="n">builder</span><span class="p">.</span><span class="n">CreateVector</span><span class="p">(</span><span class="n">treasure</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">);</span>

<span class="c1">// Construct an array of two `Vec3` structs.（构造一个包含两个 `Vec3`（三维向量结构体）的数组。）</span>
<span class="n">Vec3</span><span class="w"> </span><span class="n">points</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">Vec3</span><span class="p">(</span><span class="mf">1.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0f</span><span class="p">),</span><span class="w"> </span><span class="n">Vec3</span><span class="p">(</span><span class="mf">4.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">5.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">6.0f</span><span class="p">)</span><span class="w"> </span><span class="p">};</span>

<span class="c1">// Serialize it as a vector of structs.（将其序列化为结构体向量。）</span>
<span class="n">flatbuffers</span><span class="o">::</span><span class="n">Offset</span><span class="o">&lt;</span><span class="n">flatbuffers</span><span class="o">::</span><span class="n">Vector</span><span class="o">&lt;</span><span class="n">Vec3</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">path</span><span class="w"> </span><span class="o">=</span>
<span class="w">    </span><span class="n">builder</span><span class="p">.</span><span class="n">CreateVectorOfStructs</span><span class="p">(</span><span class="n">points</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create a `vector` representing the inventory of the Orc. Each number（创建一个代表兽人库存的 `vector（向量）`。每个数字）</span>
<span class="c1">// could correspond to an item that can be claimed after he is slain.（都可以对应一个在他被击败后可获得的物品。）</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">treasure</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">};</span>
<span class="n">flatbuffers_uint8_vec_ref_t</span><span class="w"> </span><span class="n">inventory</span><span class="p">;</span>
<span class="c1">// `c_vec_len` is the convenience macro we defined earlier.（`c_vec_len` 是我们之前定义的便捷宏。）</span>
<span class="n">inventory</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flatbuffers_uint8_vec_create</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">treasure</span><span class="p">,</span><span class="w"> </span><span class="n">c_vec_len</span><span class="p">(</span><span class="n">treasure</span><span class="p">));</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create a `vector` representing the inventory of the Orc. Each number（创建一个代表兽人库存的 `vector（向量）`。每个数字）</span>
<span class="c1">// could correspond to an item that can be claimed after he is slain.（都可以对应一个在他被击败后可获得的物品。）</span>
<span class="c1">// Note: Since we prepend the bytes, this loop iterates in reverse order.（注意：由于我们是前置字节，所以此循环以逆序方式遍历。）</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">StartInventoryVector</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">);</span>
<span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">9</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&gt;=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">--</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">builder</span><span class="p">.</span><span class="n">AddByte</span><span class="p">((</span><span class="kt">byte</span><span class="p">)</span><span class="n">i</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">Offset</span><span class="o">&lt;</span><span class="n">Vector</span><span class="o">&lt;</span><span class="kt">byte</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">inventory</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">EndVector</span><span class="p">();</span>

<span class="w">  </span><span class="c1">// Start building a path vector of length 2.（开始构建长度为2的路径向量。）</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">StartPathVector</span><span class="p">(</span><span class="n">fbb</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span>

<span class="c1">// Serialize the individual Vec3 structs（序列化每个 Vec3（向量结构体））</span>
<span class="n">Vec3</span><span class="p">.</span><span class="n">CreateVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0f</span><span class="p">);</span>
<span class="n">Vec3</span><span class="p">.</span><span class="n">CreateVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">4.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">5.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">6.0f</span><span class="p">);</span>

<span class="c1">// End the vector to get the offset（结束该向量以获得偏移量）</span>
<span class="n">Offset</span><span class="o">&lt;</span><span class="n">Vector</span><span class="o">&lt;</span><span class="n">Vec3</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">path</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fbb</span><span class="p">.</span><span class="n">EndVector</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create a list representing the inventory of the Orc. Each number（创建一个代表兽人库存的列表。每个数字）</span>
<span class="c1">// could correspond to an item that can be claimed after he is slain.（都可以对应一个在他被击败后可获得的物品。）</span>
<span class="kd">final</span><span class="w"> </span><span class="n">List</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">treasure</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="m">6</span><span class="p">,</span><span class="w"> </span><span class="m">7</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">];</span>
<span class="kd">final</span><span class="w"> </span><span class="n">inventory</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">writeListUint8</span><span class="p">(</span><span class="n">treasure</span><span class="p">);</span>

<span class="c1">// Using the Builder classes, you can write a list of structs like so:（使用 Builder 类，你可以像这样写入结构体列表：）</span>
<span class="c1">// Note that the intended order should be reversed if order is important.（注意，如果顺序很重要，目标顺序应当反转。）</span>
<span class="kd">final</span><span class="w"> </span><span class="n">vec3Builder</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">myGame</span><span class="p">.</span><span class="n">Vec3Builder</span><span class="p">(</span><span class="n">builder</span><span class="p">);</span>
<span class="n">vec3Builder</span><span class="p">.</span><span class="n">finish</span><span class="p">(</span><span class="m">4.0</span><span class="p">,</span><span class="w"> </span><span class="m">5.0</span><span class="p">,</span><span class="w"> </span><span class="m">6.0</span><span class="p">);</span>
<span class="n">vec3Builder</span><span class="p">.</span><span class="n">finish</span><span class="p">(</span><span class="m">1.0</span><span class="p">,</span><span class="w"> </span><span class="m">2.0</span><span class="p">,</span><span class="w"> </span><span class="m">3.0</span><span class="p">);</span>
<span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">path</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">endStructVector</span><span class="p">(</span><span class="m">2</span><span class="p">);</span><span class="w"> </span><span class="c1">// the length of the vector（向量的长度）</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create a `vector` representing the inventory of the Orc. Each number（创建一个代表兽人库存的 `vector（向量）`。每个数字）</span>
<span class="c1">// could correspond to an item that can be claimed after he is slain.（都可以对应一个在他被击败后可获得的物品。）</span>
<span class="c1">// Note: Since we prepend the bytes, this loop iterates in reverse.（注意：由于我们是前置字节，所以此循环以逆序方式遍历。）</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterStartInventoryVector</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span>
<span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">9</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">&gt;=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">--</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">builder</span><span class="p">.</span><span class="nx">PrependByte</span><span class="p">(</span><span class="nb">byte</span><span class="p">(</span><span class="nx">i</span><span class="p">))</span>
<span class="p">}</span>
<span class="nx">inv</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">EndVector</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>

<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterStartPathVector</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">CreateVec3</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">CreateVec3</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">4.0</span><span class="p">,</span><span class="w"> </span><span class="mf">5.0</span><span class="p">,</span><span class="w"> </span><span class="mf">6.0</span><span class="p">)</span>
<span class="nx">path</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">EndVector</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create a `vector` representing the inventory of the Orc. Each number（创建一个代表兽人库存的 `vector（向量）`。每个数字）</span>
<span class="c1">// could correspond to an item that can be claimed after he is slain.（都可以对应一个在他被击败后可获得的物品。）</span>
<span class="kt">byte</span><span class="o">[]</span><span class="w"> </span><span class="n">treasure</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">};</span>
<span class="kt">int</span><span class="w"> </span><span class="n">inv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="na">createInventoryVector</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">treasure</span><span class="p">);</span>

<span class="n">Monster</span><span class="p">.</span><span class="na">startPathVector</span><span class="p">(</span><span class="n">fbb</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span>
<span class="n">Vec3</span><span class="p">.</span><span class="na">createVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0f</span><span class="p">);</span>
<span class="n">Vec3</span><span class="p">.</span><span class="na">createVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">4.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">5.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">6.0f</span><span class="p">);</span>
<span class="kt">int</span><span class="w"> </span><span class="n">path</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fbb</span><span class="p">.</span><span class="na">endVector</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create a `vector` representing the inventory of the Orc. Each number</span>
<span class="c1">// could correspond to an item that can be claimed after he is slain.</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">treasure</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">,</span><span class="w"> </span><span class="mf">6</span><span class="p">,</span><span class="w"> </span><span class="mf">7</span><span class="p">,</span><span class="w"> </span><span class="mf">8</span><span class="p">,</span><span class="w"> </span><span class="mf">9</span><span class="p">];</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">inv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">createInventoryVector</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">treasure</span><span class="p">);</span>

<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">startPathVector</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Vec3</span><span class="p">.</span><span class="nx">createVec3</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Vec3</span><span class="p">.</span><span class="nx">createVec3</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">4.0</span><span class="p">,</span><span class="w"> </span><span class="mf">5.0</span><span class="p">,</span><span class="w"> </span><span class="mf">6.0</span><span class="p">);</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">path</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">endVector</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create a `vector` representing the inventory of the Orc. Each number</span>
<span class="c1">// could correspond to an item that can be claimed after he is slain.</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">treasure</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">byteArrayOf</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="m">6</span><span class="p">,</span><span class="w"> </span><span class="m">7</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">)</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">inv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="na">createInventoryVector</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">treasure</span><span class="p">)</span>

<span class="n">Monster</span><span class="p">.</span><span class="na">startPathVector</span><span class="p">(</span><span class="n">fbb</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">)</span>
<span class="n">Vec3</span><span class="p">.</span><span class="na">createVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="m">1.0f</span><span class="p">,</span><span class="w"> </span><span class="m">2.0f</span><span class="p">,</span><span class="w"> </span><span class="m">3.0f</span><span class="p">)</span>
<span class="n">Vec3</span><span class="p">.</span><span class="na">createVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="m">4.0f</span><span class="p">,</span><span class="w"> </span><span class="m">5.0f</span><span class="p">,</span><span class="w"> </span><span class="m">6.0f</span><span class="p">)</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">path</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fbb</span><span class="p">.</span><span class="na">endVector</span><span class="p">()</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>// Inventory.
let inv = builder.MyGame_Sample_MonsterCreateInventoryVector(map(10): _)

builder.MyGame_Sample_MonsterStartPathVector(2)
builder.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0)
builder.MyGame_Sample_CreateVec3(4.0, 5.0, 6.0)
let path = builder.EndVector(2)
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">-- Create a `vector` representing the inventory of the Orc. Each number</span>
<span class="c1">-- could correspond to an item that can be claimed after he is slain.</span>
<span class="c1">-- Note: Since we prepend the bytes, this loop iterates in reverse.</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">StartInventoryVector</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span>
<span class="kr">for</span><span class="w"> </span><span class="nv">i</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="w"> </span><span class="kr">do</span>
<span class="w">    </span><span class="nv">builder</span><span class="p">:</span><span class="nf">PrependByte</span><span class="p">(</span><span class="nv">i</span><span class="p">)</span>
<span class="kr">end</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">inv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">builder</span><span class="p">:</span><span class="nf">EndVector</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>

<span class="c1">-- Create a FlatBuffer vector and prepend the path locations.</span>
<span class="c1">-- Note: Since we prepend the data, prepend them in reverse order.</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">StartPathVector</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span>
<span class="nv">vec3</span><span class="p">.</span><span class="nf">CreateVec3</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0</span><span class="p">)</span>
<span class="nv">vec3</span><span class="p">.</span><span class="nf">CreateVec3</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">4.0</span><span class="p">,</span><span class="w"> </span><span class="mf">5.0</span><span class="p">,</span><span class="w"> </span><span class="mf">6.0</span><span class="p">)</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">path</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">builder</span><span class="p">:</span><span class="nf">EndVector</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create a `vector` representing the inventory of the Orc. Each number</span>
<span class="c1">// could correspond to an item that can be claimed after he is slain.</span>
<span class="nv">$treasure</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">);</span>
<span class="nv">$inv</span> <span class="o">=</span> <span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">CreateInventoryVector</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="nv">$treasure</span><span class="p">);</span>

<span class="nx">\MyGame\Example\Monster</span><span class="o">::</span><span class="na">StartPathVector</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
<span class="nx">\MyGame\Sample\Vec3</span><span class="o">::</span><span class="na">CreateVec3</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">);</span>
<span class="nx">\MyGame\Sample\Vec3</span><span class="o">::</span><span class="na">CreateVec3</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">);</span>
<span class="nv">$path</span> <span class="o">=</span> <span class="nv">$builder</span><span class="o">-&gt;</span><span class="na">endVector</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1"># Create a `vector` representing the inventory of the Orc. Each number</span>
<span class="c1"># could correspond to an item that can be claimed after he is slain.</span>
<span class="c1"># Note: Since we prepend the bytes, this loop iterates in reverse.</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">StartInventoryVector</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">)):</span>
    <span class="n">builder</span><span class="o">.</span><span class="n">PrependByte</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="n">inv</span> <span class="o">=</span> <span class="n">builder</span><span class="o">.</span><span class="n">EndVector</span><span class="p">()</span>

<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">StartPathVector</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Vec3</span><span class="o">.</span><span class="n">CreateVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Vec3</span><span class="o">.</span><span class="n">CreateVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">,</span> <span class="mf">5.0</span><span class="p">,</span> <span class="mf">6.0</span><span class="p">)</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">builder</span><span class="o">.</span><span class="n">EndVector</span><span class="p">()</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Inventory.</span>
<span class="kd">let</span><span class="w"> </span><span class="n">inventory</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">create_vector</span><span class="p">(</span><span class="o">&amp;</span><span class="p">[</span><span class="mi">0</span><span class="k">u8</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">]);</span>

<span class="c1">// Create the path vector of Vec3 objects.</span>
<span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Vec3</span><span class="p">::</span><span class="n">new</span><span class="p">(</span><span class="mf">1.0</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0</span><span class="p">);</span>
<span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Vec3</span><span class="p">::</span><span class="n">new</span><span class="p">(</span><span class="mf">4.0</span><span class="p">,</span><span class="w"> </span><span class="mf">5.0</span><span class="p">,</span><span class="w"> </span><span class="mf">6.0</span><span class="p">);</span>
<span class="kd">let</span><span class="w"> </span><span class="n">path</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">create_vector</span><span class="p">(</span><span class="o">&amp;</span><span class="p">[</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">]);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// create inventory</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">inventory</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="n">Byte</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">]</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">inventoryOffset</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">createVector</span><span class="p">(</span><span class="n">inventory</span><span class="p">)</span>

<span class="kd">let</span><span class="w"> </span><span class="nv">path</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">fbb</span><span class="p">.</span><span class="n">createVector</span><span class="p">(</span><span class="n">ofStructs</span><span class="p">:</span><span class="w"> </span><span class="p">[</span>
<span class="w">    </span><span class="n">Vec3</span><span class="p">(</span><span class="n">x</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">:</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">z</span><span class="p">:</span><span class="w"> </span><span class="mi">3</span><span class="p">),</span>
<span class="w">    </span><span class="n">Vec3</span><span class="p">(</span><span class="n">x</span><span class="p">:</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">:</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="n">z</span><span class="p">:</span><span class="w"> </span><span class="mi">6</span><span class="p">)</span>
<span class="p">])</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create a `vector` representing the inventory of the Orc. Each number</span>
<span class="c1">// could correspond to an item that can be claimed after he is slain.</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">treasure</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">,</span><span class="w"> </span><span class="mf">6</span><span class="p">,</span><span class="w"> </span><span class="mf">7</span><span class="p">,</span><span class="w"> </span><span class="mf">8</span><span class="p">,</span><span class="w"> </span><span class="mf">9</span><span class="p">];</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">inv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">createInventoryVector</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">treasure</span><span class="p">);</span>

<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">startPathVector</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Vec3</span><span class="p">.</span><span class="nx">createVec3</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Vec3</span><span class="p">.</span><span class="nx">createVec3</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">4.0</span><span class="p">,</span><span class="w"> </span><span class="mf">5.0</span><span class="p">,</span><span class="w"> </span><span class="mf">6.0</span><span class="p">);</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">path</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">endVector</span><span class="p">();</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <h4 id="unions">Unions（联合体）</h4>
              <p>The last non-scalar data field for the <code>Monster</code>
                table is the <code>equipped</code> <code>union</code>
                field.（<code>Monster</code> 表中的最后一个非标量数据字段是
                <code>equipped</code> <code>union（联合体）</code> 字段。）For this case,
                we will reuse an already serialized <code>Weapon</code> (the
                only
                type in the union), without needing to reserialize
                it.（在本例中，我们将复用一个已经序列化好的
                <code>Weapon（武器）</code>（联合体中唯一的类型），无需重新序列化。）Union fields
                implicitly
                add a hidden <code>_type</code> field that stores the type of
                value stored in the union.（联合体字段会隐式添加一个隐藏的 <code>_type</code>
                字段，用于存储联合体中值的类型。）When serializing a union, you must explicitly
                set this type field, along with
                providing the union value.（在序列化联合体时，必须显式设置该类型字段，并提供联合体的值。）</p>
              <p>We will also serialize the other scalar data at the same time,
                since we have all
                the necessary values and Offsets to make a
                <code>Monster</code>.（我们还会同时序列化其他标量数据，因为我们已经拥有构建
                <code>Monster</code> 所需的所有值和偏移量。）</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="9:15"><input
                  checked="checked" id="c_21" name="__tabbed_9"
                  type="radio" /><input id="c_22" name="__tabbed_9"
                  type="radio" /><input id="c_23" name="__tabbed_9"
                  type="radio" /><input id="dart_7" name="__tabbed_9"
                  type="radio" /><input id="go_7" name="__tabbed_9"
                  type="radio" /><input id="java_7" name="__tabbed_9"
                  type="radio" /><input id="javascript_7" name="__tabbed_9"
                  type="radio" /><input id="kotlin_7" name="__tabbed_9"
                  type="radio" /><input id="lobster_7" name="__tabbed_9"
                  type="radio" /><input id="lua_7" name="__tabbed_9"
                  type="radio" /><input id="php_7" name="__tabbed_9"
                  type="radio" /><input id="python_7" name="__tabbed_9"
                  type="radio" /><input id="rust_7" name="__tabbed_9"
                  type="radio" /><input id="swift_7" name="__tabbed_9"
                  type="radio" /><input id="typescript_7" name="__tabbed_9"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_21">C++</label><label for="c_22">C</label><label
                    for="c_23">C#</label><label for="dart_7">Dart</label><label
                    for="go_7">Go</label><label for="java_7">Java</label><label
                    for="javascript_7">JavaScript</label><label
                    for="kotlin_7">Kotlin</label><label
                    for="lobster_7">Lobster</label><label
                    for="lua_7">Lua</label><label for="php_7">PHP</label><label
                    for="python_7">Python</label><label
                    for="rust_7">Rust</label><label
                    for="swift_7">Swift</label><label
                    for="typescript_7">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create the remaining data needed for the Monster.</span>
<span class="k">auto</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">CreateString</span><span class="p">(</span><span class="s">&quot;Orc&quot;</span><span class="p">);</span>

<span class="c1">// Create the position struct</span>
<span class="k">auto</span><span class="w"> </span><span class="n">position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Vec3</span><span class="p">(</span><span class="mf">1.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0f</span><span class="p">);</span>

<span class="c1">// Set his hit points to 300 and his mana to 150.</span>
<span class="kt">int</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">300</span><span class="p">;</span>
<span class="kt">int</span><span class="w"> </span><span class="n">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">150</span><span class="p">;</span>

<span class="c1">// Finally, create the monster using the `CreateMonster` helper function</span>
<span class="c1">// to set all fields.</span>
<span class="c1">//</span>
<span class="c1">// Here we set the union field by using the `.Union()` method of the</span>
<span class="c1">// `Offset&lt;Weapon&gt;` axe we already serialized above. We just have to specify</span>
<span class="c1">// which type of object we put in the union, and do that with the</span>
<span class="c1">// auto-generated `Equipment_Weapon` enum.</span>
<span class="n">flatbuffers</span><span class="o">::</span><span class="n">Offset</span><span class="o">&lt;</span><span class="n">Monster</span><span class="o">&gt;</span><span class="w"> </span><span class="n">orc</span><span class="w"> </span><span class="o">=</span>
<span class="w">    </span><span class="n">CreateMonster</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">position</span><span class="p">,</span><span class="w"> </span><span class="n">mana</span><span class="p">,</span><span class="w"> </span><span class="n">hp</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="n">inventory</span><span class="p">,</span>
<span class="w">                  </span><span class="n">Color_Red</span><span class="p">,</span><span class="w"> </span><span class="n">weapons</span><span class="p">,</span><span class="w"> </span><span class="n">Equipment_Weapon</span><span class="p">,</span><span class="w"> </span><span class="n">axe</span><span class="p">.</span><span class="n">Union</span><span class="p">(),</span>
<span class="w">                  </span><span class="n">path</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Serialize a name for our monster, called &quot;Orc&quot;.</span>
<span class="c1">// The _str suffix indicates the source is an ascii-z string.</span>
<span class="n">flatbuffers_string_ref_t</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flatbuffers_string_create_str</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Orc&quot;</span><span class="p">);</span>

<span class="c1">// Set his hit points to 300 and his mana to 150.</span>
<span class="kt">uint16_t</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">300</span><span class="p">;</span>
<span class="kt">uint16_t</span><span class="w"> </span><span class="n">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">150</span><span class="p">;</span>

<span class="c1">// Define an equipment union. `create` calls in C has a single</span>
<span class="c1">// argument for unions where C++ has both a type and a data argument.</span>
<span class="n">ns</span><span class="p">(</span><span class="n">Equipment_union_ref_t</span><span class="p">)</span><span class="w"> </span><span class="n">equipped</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Equipment_as_Weapon</span><span class="p">(</span><span class="n">axe</span><span class="p">));</span>
<span class="n">ns</span><span class="p">(</span><span class="n">Vec3_t</span><span class="p">)</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mf">1.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0f</span><span class="w"> </span><span class="p">};</span>
<span class="n">ns</span><span class="p">(</span><span class="n">Monster_create_as_root</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">pos</span><span class="p">,</span><span class="w"> </span><span class="n">mana</span><span class="p">,</span><span class="w"> </span><span class="n">hp</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="n">inventory</span><span class="p">,</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Color_Red</span><span class="p">),</span>
<span class="w">        </span><span class="n">weapons</span><span class="p">,</span><span class="w"> </span><span class="n">equipped</span><span class="p">,</span><span class="w"> </span><span class="n">path</span><span class="p">));</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Create the remaining data needed for the Monster.</span>
<span class="kt">var</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">CreateString</span><span class="p">(</span><span class="s">&quot;Orc&quot;</span><span class="p">);</span>

<span class="c1">// Create our monster using `StartMonster()` and `EndMonster()`.</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">StartMonster</span><span class="p">(</span><span class="n">builder</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">AddPos</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">Vec3</span><span class="p">.</span><span class="n">CreateVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0f</span><span class="p">));</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">AddHp</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="kt">short</span><span class="p">)</span><span class="mi">300</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">AddName</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">AddInventory</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">inv</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">AddColor</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">Color</span><span class="p">.</span><span class="n">Red</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">AddWeapons</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">weapons</span><span class="p">);</span>
<span class="c1">// For union fields, we explicitly add the auto-generated enum for the type</span>
<span class="c1">// of value stored in the union.</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">AddEquippedType</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">Equipment</span><span class="p">.</span><span class="n">Weapon</span><span class="p">);</span>
<span class="c1">// And we just use the `.Value` property of the already serialized axe.</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">AddEquipped</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">axe</span><span class="p">.</span><span class="n">Value</span><span class="p">);</span><span class="w"> </span><span class="c1">// Axe</span>
<span class="n">Monster</span><span class="p">.</span><span class="n">AddPath</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">path</span><span class="p">);</span>
<span class="n">Offset</span><span class="o">&lt;</span><span class="n">Monster</span><span class="o">&gt;</span><span class="w"> </span><span class="n">orc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="n">EndMonster</span><span class="p">(</span><span class="n">builder</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Serialize a name for our monster, called &quot;Orc&quot;.</span>
<span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">writeString</span><span class="p">(</span><span class="s1">&#39;Orc&#39;</span><span class="p">);</span>

<span class="c1">// Using the Builder API:</span>
<span class="c1">// Set his hit points to 300 and his mana to 150.</span>
<span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">300</span><span class="p">;</span>
<span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">150</span><span class="p">;</span>

<span class="kd">final</span><span class="w"> </span><span class="n">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">myGame</span><span class="p">.</span><span class="n">MonsterBuilder</span><span class="p">(</span><span class="n">builder</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">begin</span><span class="p">()</span>
<span class="w">    </span><span class="p">..</span><span class="n">addNameOffset</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">addInventoryOffset</span><span class="p">(</span><span class="n">inventory</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">addWeaponsOffset</span><span class="p">(</span><span class="n">weapons</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">addEquippedType</span><span class="p">(</span><span class="n">myGame</span><span class="p">.</span><span class="n">EquipmentTypeId</span><span class="p">.</span><span class="n">Weapon</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">addEquippedOffset</span><span class="p">(</span><span class="n">axe</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">addHp</span><span class="p">(</span><span class="n">hp</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">addMana</span><span class="p">(</span><span class="n">mana</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">addPos</span><span class="p">(</span><span class="n">vec3Builder</span><span class="p">.</span><span class="n">finish</span><span class="p">(</span><span class="m">1.0</span><span class="p">,</span><span class="w"> </span><span class="m">2.0</span><span class="p">,</span><span class="w"> </span><span class="m">3.0</span><span class="p">))</span>
<span class="w">    </span><span class="p">..</span><span class="n">addPathOffset</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="w">    </span><span class="p">..</span><span class="n">addColor</span><span class="p">(</span><span class="n">myGame</span><span class="p">.</span><span class="n">Color</span><span class="p">.</span><span class="n">Red</span><span class="p">);</span>

<span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">orc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">finish</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Serialize a name for our monster, called &quot;Orc&quot;.</span>
<span class="nx">name</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">CreateString</span><span class="p">(</span><span class="s">&quot;Orc&quot;</span><span class="p">)</span>

<span class="c1">// Create our monster using `MonsterStart()` and `MonsterEnd()`.</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterStart</span><span class="p">(</span><span class="nx">builder</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterAddPos</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">sample</span><span class="p">.</span><span class="nx">CreateVec3</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0</span><span class="p">))</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterAddHp</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mi">300</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterAddName</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">name</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterAddInventory</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">inv</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterAddColor</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">sample</span><span class="p">.</span><span class="nx">ColorRed</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterAddWeapons</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">weapons</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterAddEquippedType</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">sample</span><span class="p">.</span><span class="nx">EquipmentWeapon</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterAddEquipped</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">axe</span><span class="p">)</span>
<span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterAddPath</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">)</span>
<span class="nx">orc</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">sample</span><span class="p">.</span><span class="nx">MonsterEnd</span><span class="p">(</span><span class="nx">builder</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Serialize a name for our monster, called &quot;Orc&quot;.</span>
<span class="kt">int</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="na">createString</span><span class="p">(</span><span class="s">&quot;Orc&quot;</span><span class="p">);</span>

<span class="c1">// Create our monster using `startMonster()` and `endMonster()`.</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">startMonster</span><span class="p">(</span><span class="n">builder</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addPos</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">Vec3</span><span class="p">.</span><span class="na">createVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0f</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0f</span><span class="p">));</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addName</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addColor</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">Color</span><span class="p">.</span><span class="na">Red</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addHp</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="kt">short</span><span class="p">)</span><span class="mi">300</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addInventory</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">inv</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addWeapons</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">weapons</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addEquippedType</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">Equipment</span><span class="p">.</span><span class="na">Weapon</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addEquipped</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">axe</span><span class="p">);</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addPath</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">path</span><span class="p">);</span>
<span class="kt">int</span><span class="w"> </span><span class="n">orc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="na">endMonster</span><span class="p">(</span><span class="n">builder</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Serialize a name for our monster, called &#39;Orc&#39;.</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">createString</span><span class="p">(</span><span class="s1">&#39;Orc&#39;</span><span class="p">);</span>

<span class="c1">// Create our monster by using `startMonster()` and `endMonster()`.</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">startMonster</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addPos</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span>
<span class="w">   </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Vec3</span><span class="p">.</span><span class="nx">createVec3</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0</span><span class="p">));</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addHp</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">300</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addColor</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Color</span><span class="p">.</span><span class="nx">Red</span><span class="p">)</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addName</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">name</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addInventory</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">inv</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addWeapons</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">weapons</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addEquippedType</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span>
<span class="w">    </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Equipment</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addEquipped</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">axe</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addPath</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">);</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">orc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">endMonster</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Serialize a name for our monster, called &quot;Orc&quot;.</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="na">createString</span><span class="p">(</span><span class="s">&quot;Orc&quot;</span><span class="p">)</span>

<span class="c1">// Create our monster using `startMonster()` and `endMonster()`.</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">startMonster</span><span class="p">(</span><span class="n">builder</span><span class="p">)</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addPos</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">Vec3</span><span class="p">.</span><span class="na">createVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="m">1.0f</span><span class="p">,</span><span class="w"> </span><span class="m">2.0f</span><span class="p">,</span><span class="w"> </span><span class="m">3.0f</span><span class="p">))</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addName</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="p">)</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addColor</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">Color</span><span class="p">.</span><span class="na">Red</span><span class="p">)</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addHp</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="m">300.</span><span class="n">toShort</span><span class="p">())</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addInventory</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">inv</span><span class="p">)</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addWeapons</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">weapons</span><span class="p">)</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addEquippedType</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">Equipment</span><span class="p">.</span><span class="na">Weapon</span><span class="p">)</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addEquipped</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">axe</span><span class="p">)</span>
<span class="n">Monster</span><span class="p">.</span><span class="na">addPath</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="n">path</span><span class="p">)</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">orc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="na">endMonster</span><span class="p">(</span><span class="n">builder</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>// Name of the monster.
let name = builder.CreateString(&quot;Orc&quot;)

let orc = MyGame_Sample_MonsterBuilder { b }
    .start()
    .add_pos(b.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0))
    .add_hp(300)
    .add_name(name)
    .add_inventory(inv)
    .add_color(MyGame_Sample_Color_Red)
    .add_weapons(weapons)
    .add_equipped_type(MyGame_Sample_Equipment_Weapon)
    .add_equipped(weapon_offsets[1])
    .add_path(path)
    .end()
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">-- Serialize a name for our monster, called &#39;orc&#39;</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">builder</span><span class="p">:</span><span class="nf">CreateString</span><span class="p">(</span><span class="s2">&quot;Orc&quot;</span><span class="p">)</span>

<span class="c1">-- Create our monster by using Start() andEnd()</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">Start</span><span class="p">(</span><span class="nv">builder</span><span class="p">)</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">AddPos</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="nv">vec3</span><span class="p">.</span><span class="nf">CreateVec3</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0</span><span class="p">))</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">AddHp</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="mi">300</span><span class="p">)</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">AddName</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="nv">name</span><span class="p">)</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">AddInventory</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="nv">inv</span><span class="p">)</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">AddColor</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="nv">color</span><span class="p">.</span><span class="py">Red</span><span class="p">)</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">AddWeapons</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="nv">weapons</span><span class="p">)</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">AddEquippedType</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="nv">equipment</span><span class="p">.</span><span class="py">Weapon</span><span class="p">)</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">AddEquipped</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="nv">axe</span><span class="p">)</span>
<span class="nv">monster</span><span class="p">.</span><span class="nf">AddPath</span><span class="p">(</span><span class="nv">builder</span><span class="p">,</span><span class="w"> </span><span class="nv">path</span><span class="p">)</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">orc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">monster</span><span class="p">.</span><span class="nf">End</span><span class="p">(</span><span class="nv">builder</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Serialize a name for our monster, called &quot;Orc&quot;.</span>
<span class="nv">$name</span> <span class="o">=</span> <span class="nv">$builder</span><span class="o">-&gt;</span><span class="na">createString</span><span class="p">(</span><span class="s2">&quot;Orc&quot;</span><span class="p">);</span>

    <span class="c1">// Create our monster by using `StartMonster()` and `EndMonster()`.</span>
<span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">StartMonster</span><span class="p">(</span><span class="nv">$builder</span><span class="p">);</span>
<span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">AddPos</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span>
    <span class="nx">\MyGame\Sample\Vec3</span><span class="o">::</span><span class="na">CreateVec3</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">));</span>
<span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">AddHp</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="mi">300</span><span class="p">);</span>
<span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">AddName</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="nv">$name</span><span class="p">);</span>
<span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">AddInventory</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="nv">$inv</span><span class="p">);</span>
<span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">AddColor</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="nx">\MyGame\Sample\Color</span><span class="o">::</span><span class="na">Red</span><span class="p">);</span>
<span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">AddWeapons</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="nv">$weapons</span><span class="p">);</span>
<span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">AddEquippedType</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> 
    <span class="nx">\MyGame\Sample\Equipment</span><span class="o">::</span><span class="na">Weapon</span><span class="p">);</span>
<span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">AddEquipped</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="nv">$axe</span><span class="p">);</span>
<span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">AddPath</span><span class="p">(</span><span class="nv">$builder</span><span class="p">,</span> <span class="nv">$path</span><span class="p">);</span>
<span class="nv">$orc</span> <span class="o">=</span> <span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">EndMonster</span><span class="p">(</span><span class="nv">$builder</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1"># Serialize a name for our monster, called &quot;Orc&quot;.</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">builder</span><span class="o">.</span><span class="n">CreateString</span><span class="p">(</span><span class="s2">&quot;Orc&quot;</span><span class="p">)</span>

<span class="c1"># Create our monster by using `Monster.Start()` and `Monster.End()`.</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">Start</span><span class="p">(</span><span class="n">builder</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">AddPos</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span>
    <span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Vec3</span><span class="o">.</span><span class="n">CreateVec3</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">))</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">AddHp</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="mi">300</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">AddName</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">AddInventory</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="n">inv</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">AddColor</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span>
                                        <span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Color</span><span class="o">.</span><span class="n">Color</span><span class="p">()</span><span class="o">.</span><span class="n">Red</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">AddWeapons</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="n">weapons</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">AddEquippedType</span><span class="p">(</span>
    <span class="n">builder</span><span class="p">,</span> <span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Equipment</span><span class="o">.</span><span class="n">Equipment</span><span class="p">()</span><span class="o">.</span><span class="n">Weapon</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">AddEquipped</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="n">axe</span><span class="p">)</span>
<span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">AddPath</span><span class="p">(</span><span class="n">builder</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>
<span class="n">orc</span> <span class="o">=</span> <span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">End</span><span class="p">(</span><span class="n">builder</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Name of the Monster.</span>
<span class="kd">let</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">create_string</span><span class="p">(</span><span class="s">&quot;Orc&quot;</span><span class="p">);</span>

<span class="c1">// Create the monster using the `Monster::create` helper function. This</span>
<span class="c1">// function accepts a `MonsterArgs` struct, which supplies all of the data</span>
<span class="c1">// needed to build a `Monster`. To supply empty/default fields, just use the</span>
<span class="c1">// Rust built-in `Default::default()` function, as demonstrated below.</span>
<span class="kd">let</span><span class="w"> </span><span class="n">orc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">::</span><span class="n">create</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">builder</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">MonsterArgs</span><span class="p">{</span>
<span class="w">    </span><span class="n">pos</span><span class="p">:</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="o">&amp;</span><span class="n">Vec3</span><span class="p">::</span><span class="n">new</span><span class="p">(</span><span class="mf">1.0</span><span class="k">f32</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="k">f32</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0</span><span class="k">f32</span><span class="p">)),</span>
<span class="w">    </span><span class="n">mana</span><span class="p">:</span><span class="w"> </span><span class="mi">150</span><span class="p">,</span>
<span class="w">    </span><span class="n">hp</span><span class="p">:</span><span class="w"> </span><span class="mi">80</span><span class="p">,</span>
<span class="w">    </span><span class="n">name</span><span class="p">:</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
<span class="w">    </span><span class="n">inventory</span><span class="p">:</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">inventory</span><span class="p">),</span>
<span class="w">    </span><span class="n">color</span><span class="p">:</span><span class="w"> </span><span class="nc">Color</span><span class="p">::</span><span class="n">Red</span><span class="p">,</span>
<span class="w">    </span><span class="n">weapons</span><span class="p">:</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">weapons</span><span class="p">),</span>
<span class="w">    </span><span class="n">equipped_type</span><span class="p">:</span><span class="w"> </span><span class="nc">Equipment</span><span class="p">::</span><span class="n">Weapon</span><span class="p">,</span>
<span class="w">    </span><span class="n">equipped</span><span class="p">:</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">axe</span><span class="p">.</span><span class="n">as_union_value</span><span class="p">()),</span>
<span class="w">    </span><span class="n">path</span><span class="p">:</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">path</span><span class="p">),</span>
<span class="w">    </span><span class="o">..</span><span class="nb">Default</span><span class="p">::</span><span class="n">default</span><span class="p">()</span>
<span class="p">});</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Name of the Monster.</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">name</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">create</span><span class="p">(</span><span class="n">string</span><span class="p">:</span><span class="w"> </span><span class="s">&quot;Orc&quot;</span><span class="p">)</span>

<span class="kd">let</span><span class="w"> </span><span class="nv">orc</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="n">createMonster</span><span class="p">(</span>
<span class="w">    </span><span class="p">&amp;</span><span class="n">builder</span><span class="p">,</span>
<span class="w">    </span><span class="n">pos</span><span class="p">:</span><span class="w"> </span><span class="n">MyGame_Sample_Vec3</span><span class="p">(</span><span class="n">x</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">:</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">z</span><span class="p">:</span><span class="w"> </span><span class="mi">3</span><span class="p">),</span>
<span class="w">    </span><span class="n">hp</span><span class="p">:</span><span class="w"> </span><span class="mi">300</span><span class="p">,</span>
<span class="w">    </span><span class="n">nameOffset</span><span class="p">:</span><span class="w"> </span><span class="n">name</span><span class="p">,</span>
<span class="w">    </span><span class="n">inventoryVectorOffset</span><span class="p">:</span><span class="w"> </span><span class="n">inventoryOffset</span><span class="p">,</span>
<span class="w">    </span><span class="n">color</span><span class="p">:</span><span class="w"> </span><span class="p">.</span><span class="n">red</span><span class="p">,</span>
<span class="w">    </span><span class="n">weaponsVectorOffset</span><span class="p">:</span><span class="w"> </span><span class="n">weaponsOffset</span><span class="p">,</span>
<span class="w">    </span><span class="n">equippedType</span><span class="p">:</span><span class="w"> </span><span class="p">.</span><span class="n">weapon</span><span class="p">,</span>
<span class="w">    </span><span class="n">equippedOffset</span><span class="p">:</span><span class="w"> </span><span class="n">axe</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Serialize a name for our monster, called &#39;Orc&#39;.</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">createString</span><span class="p">(</span><span class="s1">&#39;Orc&#39;</span><span class="p">);</span>

<span class="c1">// Create our monster by using `startMonster()` and `endMonster()`.</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">startMonster</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addPos</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span>
<span class="w">    </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Vec3</span><span class="p">.</span><span class="nx">createVec3</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="mf">3.0</span><span class="p">));</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addHp</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="mf">300</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addColor</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Color</span><span class="p">.</span><span class="nx">Red</span><span class="p">)</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addName</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">name</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addInventory</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">inv</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addWeapons</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">weapons</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addEquippedType</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span>
<span class="w">    </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Equipment</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addEquipped</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">axe</span><span class="p">);</span>
<span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">addPath</span><span class="p">(</span><span class="nx">builder</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">);</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">orc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">endMonster</span><span class="p">(</span><span class="nx">builder</span><span class="p">);</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <h3 id="finishing">Finishing（结束/完成）</h3>
              <p>At this point, we have serialized a <code>Monster</code> we've
                named "orc" to the
                flatbuffer and have its offset.（此时，我们已经将名为 "orc" 的
                <code>Monster（怪物）</code> 序列化到 flatbuffer（扁平缓冲区）中，并获得了它的偏移量。）The
                <code>root_type</code> of the schema is also a
                <code>Monster</code>, so we have everything we need to finish
                the serialization step.（该 schema（模式）的
                <code>root_type（根类型）</code> 也是
                <code>Monster</code>，因此我们已经具备完成序列化步骤所需的一切。）</p>
              <p>This is done by calling the appropriate <code>finish</code>
                method on the Builder, passing
                in the orc offset to indicate this <code>table</code> is the
                "entry" point when
                deserializing the buffer later.（这可以通过在 Builder（构建器）上调用相应的
                <code>finish（完成）</code> 方法来实现，并传入 orc 的偏移量，以指示该
                <code>table（表）</code> 是后续反序列化缓冲区时的“入口”点。）</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="10:15"><input
                  checked="checked" id="c_24" name="__tabbed_10"
                  type="radio" /><input id="c_25" name="__tabbed_10"
                  type="radio" /><input id="c_26" name="__tabbed_10"
                  type="radio" /><input id="dart_8" name="__tabbed_10"
                  type="radio" /><input id="go_8" name="__tabbed_10"
                  type="radio" /><input id="java_8" name="__tabbed_10"
                  type="radio" /><input id="javascript_8" name="__tabbed_10"
                  type="radio" /><input id="kotlin_8" name="__tabbed_10"
                  type="radio" /><input id="lobster_8" name="__tabbed_10"
                  type="radio" /><input id="lua_8" name="__tabbed_10"
                  type="radio" /><input id="php_8" name="__tabbed_10"
                  type="radio" /><input id="python_8" name="__tabbed_10"
                  type="radio" /><input id="rust_8" name="__tabbed_10"
                  type="radio" /><input id="swift_8" name="__tabbed_10"
                  type="radio" /><input id="typescript_8" name="__tabbed_10"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_24">C++</label><label for="c_25">C</label><label
                    for="c_26">C#</label><label for="dart_8">Dart</label><label
                    for="go_8">Go</label><label for="java_8">Java</label><label
                    for="javascript_8">JavaScript</label><label
                    for="kotlin_8">Kotlin</label><label
                    for="lobster_8">Lobster</label><label
                    for="lua_8">Lua</label><label for="php_8">PHP</label><label
                    for="python_8">Python</label><label
                    for="rust_8">Rust</label><label
                    for="swift_8">Swift</label><label
                    for="typescript_8">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Call `Finish()` to instruct the builder that this monster is complete.</span>
<span class="c1">// You could also call `FinishMonsterBuffer(builder, orc);`</span>
<span class="n">builder</span><span class="p">.</span><span class="n">Finish</span><span class="p">(</span><span class="n">orc</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Because we used `Monster_create_as_root`, we do not need a `finish` call</span>
<span class="c1">// in C.</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Call `Finish()` to instruct the builder that this monster is complete.</span>
<span class="c1">// You could also call `Monster.FinishMonsterBuffer(builder, orc);`</span>
<span class="n">builder</span><span class="p">.</span><span class="n">Finish</span><span class="p">(</span><span class="n">orc</span><span class="p">.</span><span class="n">Value</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Call `finish()` to instruct the builder that this monster is complete.</span>
<span class="c1">// See the next code section, as in Dart `finish` will also return the byte </span>
<span class="c1">// array.</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Call `Finish()` to instruct the builder that this monster is complete.</span>
<span class="nx">builder</span><span class="p">.</span><span class="nx">Finish</span><span class="p">(</span><span class="nx">orc</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Call `finish()` to instruct the builder that this monster is complete.</span>
<span class="n">builder</span><span class="p">.</span><span class="na">finish</span><span class="p">(</span><span class="n">orc</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Call `finish()` to instruct the builder that this monster is complete.</span>
<span class="nx">builder</span><span class="p">.</span><span class="nx">finish</span><span class="p">(</span><span class="nx">orc</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Call `finish()` to instruct the builder that this monster is complete.</span>
<span class="n">builder</span><span class="p">.</span><span class="na">finish</span><span class="p">(</span><span class="n">orc</span><span class="p">)</span><span class="w"> </span><span class="p">;</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>// Call `Finish()` to instruct the builder that this monster is complete.
builder.Finish(orc)
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">-- Call &#39;Finish()&#39; to instruct the builder that this monster is complete.</span>
<span class="nv">builder</span><span class="p">:</span><span class="nf">Finish</span><span class="p">(</span><span class="nv">orc</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Call `finish()` to instruct the builder that this monster is complete.</span>
<span class="nv">$builder</span><span class="o">-&gt;</span><span class="na">finish</span><span class="p">(</span><span class="nv">$orc</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1"># Call `Finish()` to instruct the builder that this monster is complete.</span>
<span class="n">builder</span><span class="o">.</span><span class="n">Finish</span><span class="p">(</span><span class="n">orc</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Call `finish()` to instruct the builder that this monster is complete.</span>
<span class="n">builder</span><span class="p">.</span><span class="n">finish</span><span class="p">(</span><span class="n">orc</span><span class="p">,</span><span class="w"> </span><span class="nb">None</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Call `finish()` to instruct the builder that this monster is complete.</span>
<span class="n">builder</span><span class="p">.</span><span class="n">finish</span><span class="p">(</span><span class="n">offset</span><span class="p">:</span><span class="w"> </span><span class="n">orc</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Call `finish()` to instruct the builder that this monster is complete.</span>
<span class="nx">builder</span><span class="p">.</span><span class="nx">finish</span><span class="p">(</span><span class="nx">orc</span><span class="p">);</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>Once you finish a Builder, you can no longer serialize more
                data to it.（一旦你完成了一个 Builder（构建器），就无法再向其序列化更多数据了。）</p>
              <h4 id="buffer-access">Buffer Access（缓冲区访问）</h4>
              <p>The flatbuffer is now ready to be stored somewhere, sent over
                the network,
                compressed, or whatever you would like to do with it.（现在
                flatbuffer（扁平缓冲区）已经可以被存储到某处、通过网络发送、压缩，或用于你想要的任何操作。）You access
                the raw buffer
                like so:（你可以这样访问原始缓冲区：）</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="11:15"><input
                  checked="checked" id="c_27" name="__tabbed_11"
                  type="radio" /><input id="c_28" name="__tabbed_11"
                  type="radio" /><input id="c_29" name="__tabbed_11"
                  type="radio" /><input id="dart_9" name="__tabbed_11"
                  type="radio" /><input id="go_9" name="__tabbed_11"
                  type="radio" /><input id="java_9" name="__tabbed_11"
                  type="radio" /><input id="javascript_9" name="__tabbed_11"
                  type="radio" /><input id="kotlin_9" name="__tabbed_11"
                  type="radio" /><input id="lobster_9" name="__tabbed_11"
                  type="radio" /><input id="lua_9" name="__tabbed_11"
                  type="radio" /><input id="php_9" name="__tabbed_11"
                  type="radio" /><input id="python_9" name="__tabbed_11"
                  type="radio" /><input id="rust_9" name="__tabbed_11"
                  type="radio" /><input id="swift_9" name="__tabbed_11"
                  type="radio" /><input id="typescript_9" name="__tabbed_11"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_27">C++</label><label for="c_28">C</label><label
                    for="c_29">C#</label><label for="dart_9">Dart</label><label
                    for="go_9">Go</label><label for="java_9">Java</label><label
                    for="javascript_9">JavaScript</label><label
                    for="kotlin_9">Kotlin</label><label
                    for="lobster_9">Lobster</label><label
                    for="lua_9">Lua</label><label for="php_9">PHP</label><label
                    for="python_9">Python</label><label
                    for="rust_9">Rust</label><label
                    for="swift_9">Swift</label><label
                    for="typescript_9">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// This must be called after `Finish()`.</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="o">*</span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">GetBufferPointer</span><span class="p">();</span>

<span class="c1">// Returns the size of the buffer that `GetBufferPointer()` points to.</span>
<span class="kt">int</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">GetSize</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">uint8_t</span><span class="w"> </span><span class="o">*</span><span class="n">buf</span><span class="p">;</span>
<span class="kt">size_t</span><span class="w"> </span><span class="n">size</span><span class="p">;</span>

<span class="c1">// Allocate and extract a readable buffer from internal builder heap.</span>
<span class="c1">// The returned buffer must be deallocated using `free`.</span>
<span class="c1">// NOTE: Finalizing the buffer does NOT change the builder, it</span>
<span class="c1">// just creates a snapshot of the builder content.</span>
<span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flatcc_builder_finalize_buffer</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">size</span><span class="p">);</span>
<span class="c1">// use buf</span>
<span class="n">free</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span>

<span class="c1">// Optionally reset builder to reuse builder without deallocating</span>
<span class="c1">// internal stack and heap.</span>
<span class="n">flatcc_builder_reset</span><span class="p">(</span><span class="n">B</span><span class="p">);</span>
<span class="c1">// build next buffer.</span>
<span class="c1">// ...</span>

<span class="c1">// Cleanup.</span>
<span class="n">flatcc_builder_clear</span><span class="p">(</span><span class="n">B</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// This must be called after `Finish()`.</span>
<span class="c1">//</span>
<span class="c1">// The data in this ByteBuffer does NOT start at 0, but at buf.Position.</span>
<span class="c1">// The end of the data is marked by buf.Length, so the size is</span>
<span class="c1">// buf.Length - buf.Position.</span>
<span class="n">FlatBuffers</span><span class="p">.</span><span class="n">ByteBuffer</span><span class="w"> </span><span class="n">dataBuffer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">DataBuffer</span><span class="p">;</span>

<span class="c1">// Alternatively this copies the above data out of the ByteBuffer for you:</span>
<span class="kt">byte</span><span class="p">[]</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">SizedByteArray</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">final</span><span class="w"> </span><span class="n">Uint8List</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">finish</span><span class="p">(</span><span class="n">orc</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// This must be called after `Finish()`.</span>
<span class="nx">buf</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">FinishedBytes</span><span class="p">()</span><span class="w"> </span><span class="c1">// Of type `byte[]`.</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// This must be called after `finish()`.</span>
<span class="n">java</span><span class="p">.</span><span class="na">nio</span><span class="p">.</span><span class="na">ByteBuffer</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="na">dataBuffer</span><span class="p">();</span>
<span class="c1">// The data in this ByteBuffer does NOT start at 0, but at buf.position().</span>
<span class="c1">// The number of bytes is buf.remaining().</span>

<span class="c1">// Alternatively this copies the above data out of the ByteBuffer for you:</span>
<span class="kt">byte</span><span class="o">[]</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="na">sizedByteArray</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// This must be called after `finish()`.</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">asUint8Array</span><span class="p">();</span><span class="w"> </span><span class="c1">// Of type `Uint8Array`.</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// This must be called after `finish()`.</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="na">dataBuffer</span><span class="p">()</span>
<span class="c1">// The data in this ByteBuffer does NOT start at 0, but at buf.position().</span>
<span class="c1">// The number of bytes is buf.remaining().</span>

<span class="c1">// Alternatively this copies the above data out of the ByteBuffer for you:</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="na">sizedByteArray</span><span class="p">()</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>// This must be called after `Finish()`.
let buf = builder.SizedCopy() // Of type `string`.
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">local</span><span class="w"> </span><span class="nv">bufAsString</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">builder</span><span class="p">:</span><span class="nf">Output</span><span class="p">()</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// This must be called after `finish()`.</span>
<span class="nv">$buf</span> <span class="o">=</span> <span class="nv">$builder</span><span class="o">-&gt;</span><span class="na">dataBuffer</span><span class="p">();</span> <span class="c1">// Of type `Google\FlatBuffers\ByteBuffer`</span>
<span class="c1">// The data in this ByteBuffer does NOT start at 0, but at </span>
<span class="c1">// buf-&gt;getPosition().</span>
<span class="c1">// The end of the data is marked by buf-&gt;capacity(), so the size is</span>
<span class="c1">// buf-&gt;capacity() - buf-&gt;getPosition().</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1"># This must be called after `Finish()`.</span>
<span class="n">buf</span> <span class="o">=</span> <span class="n">builder</span><span class="o">.</span><span class="n">Output</span><span class="p">()</span> <span class="o">//</span> <span class="n">Of</span> <span class="nb">type</span> <span class="err">`</span><span class="nb">bytearray</span><span class="err">`</span><span class="o">.</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// This must be called after `finish()`.</span>
<span class="c1">// `finished_data` returns a byte slice.</span>
<span class="kd">let</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">finished_data</span><span class="p">();</span><span class="w"> </span><span class="c1">// Of type `&amp;[u8]`</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// This must be called after `finish()`.</span>
<span class="c1">// `sizedByteArray` returns the finished buf of type [UInt8].</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">buf</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">sizedByteArray</span>
<span class="c1">// or you can use to get an object of type Data</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">bufData</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ByteBuffer</span><span class="p">(</span><span class="n">data</span><span class="p">:</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">data</span><span class="p">)</span>
<span class="c1">// or</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">buf</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">sizedBuffer</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// This must be called after `finish()`.</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">builder</span><span class="p">.</span><span class="nx">asUint8Array</span><span class="p">();</span><span class="w"> </span><span class="c1">// Of type `Uint8Array`.</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>Now you can write the bytes to a file or send them over the
                network. The buffer
                stays valid until the Builder is cleared or
                destroyed.（现在你可以将字节写入文件或通过网络发送。在
                Builder（构建器）被清除或销毁之前，缓冲区始终有效。）</p>
              <p>Make sure your file mode (or transfer protocol) is set to
                BINARY, and not TEXT.（请确保你的文件模式（或传输协议）设置为 BINARY（二进制），而不是
                TEXT（文本）。）<br>
                If you try to transfer a flatbuffer in TEXT mode, the buffer
                will be corrupted
                and be hard to diagnose.（如果你尝试以文本模式传输
                flatbuffer（扁平缓冲区），缓冲区会被破坏，并且难以排查问题。）</p>
              <h2 id="deserialization">Deserialization（反序列化）</h2>
              <p>Deserialization is a bit of a misnomer, since FlatBuffers
                doesn't deserialize
                the whole buffer when accessed.（反序列化这个词其实有些用词不当，因为 FlatBuffers
                在访问时并不会反序列化整个缓冲区。）It just "decodes" the data that is requested,
                leaving all the other data untouched.（它只会“解码”被请求的数据，其他数据保持不变。）It
                is up to the application to decide if
                the data is copied out or even read in the first
                place.（是否将数据拷贝出来，甚至是否读取，完全由应用程序决定。）However, we continue to
                use the word <code>deserialize</code> to mean accessing data
                from a binary flatbuffer.（不过，我们依然用
                <code>deserialize（反序列化）</code> 这个词来表示从二进制
                flatbuffer（扁平缓冲区）中访问数据。）</p>
              <p>Now that we have successfully create an orc FlatBuffer, the
                data can be saved,
                sent over a network, etc.（现在我们已经成功创建了一个
                orc（兽人）FlatBuffer，这些数据可以被保存、通过网络发送等。）At some point, the buffer
                will be accessed to obtain
                the underlying data.（在某个时刻，缓冲区会被访问以获取底层数据。）</p>
              <p>The same application setup used for serialization is needed for
                deserialization
                (see <a href="#application-integration">application
                  integration（应用集成）</a>).
                （反序列化时需要与序列化相同的应用程序设置（参见 <a
                  href="#application-integration">应用集成</a>）。）</p>
              <h3 id="root-access">Root Access（根对象访问）</h3>
              <p>All access to the data in the flatbuffer must first go through
                the root object.（对 flatbuffer（扁平缓冲区）中数据的所有访问都必须首先通过根对象进行。）There
                is only one root object per flatbuffer.（每个 flatbuffer
                只有一个根对象。）The generated code provides
                functions to get the root object given the
                buffer.（生成的代码会提供根据缓冲区获取根对象的函数。）</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="12:15"><input
                  checked="checked" id="c_30" name="__tabbed_12"
                  type="radio" /><input id="c_31" name="__tabbed_12"
                  type="radio" /><input id="c_32" name="__tabbed_12"
                  type="radio" /><input id="dart_10" name="__tabbed_12"
                  type="radio" /><input id="go_10" name="__tabbed_12"
                  type="radio" /><input id="java_10" name="__tabbed_12"
                  type="radio" /><input id="javascript_10" name="__tabbed_12"
                  type="radio" /><input id="kotlin_10" name="__tabbed_12"
                  type="radio" /><input id="lobster_10" name="__tabbed_12"
                  type="radio" /><input id="lua_10" name="__tabbed_12"
                  type="radio" /><input id="php_10" name="__tabbed_12"
                  type="radio" /><input id="python_10" name="__tabbed_12"
                  type="radio" /><input id="rust_10" name="__tabbed_12"
                  type="radio" /><input id="swift_10" name="__tabbed_12"
                  type="radio" /><input id="typescript_10" name="__tabbed_12"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_30">C++</label><label for="c_31">C</label><label
                    for="c_32">C#</label><label for="dart_10">Dart</label><label
                    for="go_10">Go</label><label
                    for="java_10">Java</label><label
                    for="javascript_10">JavaScript</label><label
                    for="kotlin_10">Kotlin</label><label
                    for="lobster_10">Lobster</label><label
                    for="lua_10">Lua</label><label
                    for="php_10">PHP</label><label
                    for="python_10">Python</label><label
                    for="rust_10">Rust</label><label
                    for="swift_10">Swift</label><label
                    for="typescript_10">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">uint8_t</span><span class="w"> </span><span class="o">*</span><span class="n">buffer_pointer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="cm">/* the data you just read */</span><span class="p">;</span>

<span class="c1">// Get an view to the root object inside the buffer.</span>
<span class="n">Monster</span><span class="w"> </span><span class="n">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">GetMonster</span><span class="p">(</span><span class="n">buffer_pointer</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Note that we use the `table_t` suffix when reading a table object</span>
<span class="c1">// as opposed to the `ref_t` suffix used during the construction of</span>
<span class="c1">// the buffer.</span>
<span class="n">ns</span><span class="p">(</span><span class="n">Monster_table_t</span><span class="p">)</span><span class="w"> </span><span class="n">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Monster_as_root</span><span class="p">(</span><span class="n">buffer</span><span class="p">));</span>

<span class="c1">// Note: root object pointers are NOT the same as the `buffer` pointer.</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">byte</span><span class="p">[]</span><span class="w"> </span><span class="n">bytes</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="cm">/* the data you just read */</span>

<span class="c1">// Get an view to the root object inside the buffer.</span>
<span class="n">Monster</span><span class="w"> </span><span class="n">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="n">GetRootAsMonster</span><span class="p">(</span><span class="k">new</span><span class="w"> </span><span class="n">ByteBuffer</span><span class="p">(</span><span class="n">bytes</span><span class="p">));</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">List</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="c1">// the data, e.g. from file or network</span>
<span class="c1">// A generated factory constructor that will read the data.</span>
<span class="n">myGame</span><span class="p">.</span><span class="n">Monster</span><span class="w"> </span><span class="n">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">myGame</span><span class="p">.</span><span class="n">Monster</span><span class="p">(</span><span class="n">data</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">buf</span><span class="w"> </span><span class="p">[]</span><span class="kt">byte</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="cm">/* the data you just read */</span>

<span class="c1">// Get an accessor to the root object inside the buffer.</span>
<span class="nx">monster</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">sample</span><span class="p">.</span><span class="nx">GetRootAsMonster</span><span class="p">(</span><span class="nx">buf</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>

<span class="c1">// Note: We use `0` for the offset here, which is typical for most buffers</span>
<span class="c1">// you would read. If you wanted to read from `builder.Bytes` directly, you</span>
<span class="c1">// would need to pass in the offset of `builder.Head()`, as the builder</span>
<span class="c1">// constructs the buffer backwards, so may not start at offset 0.</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">byte</span><span class="o">[]</span><span class="w"> </span><span class="n">bytes</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="cm">/* the data you just read */</span>
<span class="n">java</span><span class="p">.</span><span class="na">nio</span><span class="p">.</span><span class="na">ByteBuffer</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">java</span><span class="p">.</span><span class="na">nio</span><span class="p">.</span><span class="na">ByteBuffer</span><span class="p">.</span><span class="na">wrap</span><span class="p">(</span><span class="n">bytes</span><span class="p">);</span>

<span class="c1">// Get an accessor to the root object inside the buffer.</span>
<span class="n">Monster</span><span class="w"> </span><span class="n">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="na">getRootAsMonster</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// the data you just read, as a `Uint8Array`</span>
<span class="c1">// Note that the example here uses `readFileSync` from the built-in `fs`</span>
<span class="c1">// module, but other methods for accessing the file contents will also work.</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">bytes</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Uint8Array</span><span class="p">(</span><span class="nx">readFileSync</span><span class="p">(</span><span class="s1">&#39;./monsterdata.bin&#39;</span><span class="p">));</span>

<span class="kd">var</span><span class="w"> </span><span class="nx">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">flatbuffers</span><span class="p">.</span><span class="nx">ByteBuffer</span><span class="p">(</span><span class="nx">bytes</span><span class="p">);</span>

<span class="c1">// Get an accessor to the root object inside the buffer.</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">getRootAsMonster</span><span class="p">(</span><span class="nx">buf</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">val</span><span class="w"> </span><span class="nv">bytes</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="cm">/* the data you just read */</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">java</span><span class="p">.</span><span class="na">nio</span><span class="p">.</span><span class="na">ByteBuffer</span><span class="p">.</span><span class="na">wrap</span><span class="p">(</span><span class="n">bytes</span><span class="p">)</span>

<span class="c1">// Get an accessor to the root object inside the buffer.</span>
<span class="n">Monster</span><span class="w"> </span><span class="n">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="na">getRootAsMonster</span><span class="p">(</span><span class="n">buf</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>buf = /* the data you just read, in a string */

// Get an accessor to the root object inside the buffer.
let monster = MyGame_Sample_GetRootAsMonster(buf)
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">local</span><span class="w"> </span><span class="nv">bufAsString</span><span class="w"> </span><span class="o">=</span><span class="w">   </span><span class="c1">-- The data you just read in</span>

<span class="c1">-- Convert the string representation into binary array Lua structure</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">flatbuffers</span><span class="p">.</span><span class="py">binaryArray</span><span class="p">.</span><span class="nf">New</span><span class="p">(</span><span class="nv">bufAsString</span><span class="p">)</span>

<span class="c1">-- Get an accessor to the root object insert the buffer</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">mon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">monster</span><span class="p">.</span><span class="nf">GetRootAsMonster</span><span class="p">(</span><span class="nv">buf</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nv">$bytes</span> <span class="o">=</span> <span class="cm">/* the data you just read, in a string */</span>
<span class="nv">$buf</span> <span class="o">=</span> <span class="nx">Google\FlatBuffers\ByteBuffer</span><span class="o">::</span><span class="na">wrap</span><span class="p">(</span><span class="nv">$bytes</span><span class="p">);</span>

<span class="c1">// Get an accessor to the root object inside the buffer.</span>
<span class="nv">$monster</span> <span class="o">=</span> <span class="nx">\MyGame\Sample\Monster</span><span class="o">::</span><span class="na">GetRootAsMonster</span><span class="p">(</span><span class="nv">$buf</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">buf</span> <span class="o">=</span> <span class="o">/*</span> <span class="n">the</span> <span class="n">data</span> <span class="n">you</span> <span class="n">just</span> <span class="n">read</span><span class="p">,</span> <span class="ow">in</span> <span class="n">an</span> <span class="nb">object</span> <span class="n">of</span> <span class="nb">type</span> <span class="s2">&quot;bytearray&quot;</span> <span class="o">*/</span>

<span class="c1"># Get an accessor to the root object inside the buffer.</span>
<span class="n">monster</span> <span class="o">=</span> <span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">Monster</span><span class="o">.</span><span class="n">GetRootAs</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

<span class="c1"># Note: We use `0` for the offset here, which is typical for most buffers</span>
<span class="c1"># you would read.  If you wanted to read from the `builder.Bytes` directly,</span>
<span class="c1"># you would need to pass in the offset of `builder.Head()`, as the builder</span>
<span class="c1"># constructs the buffer backwards, so may not start at offset 0.</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="cm">/* the data you just read, in a &amp;[u8] */</span>

<span class="c1">// Get an accessor to the root object inside the buffer.</span>
<span class="kd">let</span><span class="w"> </span><span class="n">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">root_as_monster</span><span class="p">(</span><span class="n">buf</span><span class="p">).</span><span class="n">unwrap</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// create a ByteBuffer(:) from an [UInt8] or Data()</span>
<span class="kd">var</span><span class="w"> </span><span class="nv">buf</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="c1">// Get your data</span>
<span class="c1">// Get an accessor to the root object inside the buffer.</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">monster</span><span class="p">:</span><span class="w"> </span><span class="n">Monster</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">try</span><span class="p">!</span><span class="w"> </span><span class="n">getCheckedRoot</span><span class="p">(</span><span class="n">byteBuffer</span><span class="p">:</span><span class="w"> </span><span class="p">&amp;</span><span class="n">byteBuffer</span><span class="p">)</span>
<span class="c1">// let monster: Monster = getRoot(byteBuffer: &amp;byteBuffer)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// the data you just read, as a `Uint8Array`.</span>
<span class="c1">// Note that the example here uses `readFileSync` from the built-in `fs` </span>
<span class="c1">// module, but other methods for accessing the file contents will also work.</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">bytes</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Uint8Array</span><span class="p">(</span><span class="nx">readFileSync</span><span class="p">(</span><span class="s1">&#39;./monsterdata.bin&#39;</span><span class="p">));</span>

<span class="kd">let</span><span class="w"> </span><span class="nx">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">flatbuffers</span><span class="p">.</span><span class="nx">ByteBuffer</span><span class="p">(</span><span class="nx">bytes</span><span class="p">);</span>

<span class="c1">// Get an accessor to the root object inside the buffer.</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Monster</span><span class="p">.</span><span class="nx">getRootAsMonster</span><span class="p">(</span><span class="nx">buf</span><span class="p">);</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>Again, make sure you read the bytes in BINARY mode, otherwise
                the buffer may be
                corrupted.（再次提醒，请确保以 BINARY（二进制）模式读取字节，否则缓冲区可能会被破坏。）</p>
              <p>In most languages, the returned object is just a "view" of the
                data with helpful
                accessors.（在大多数编程语言中，返回的对象只是数据的一个“视图”，并带有便捷的访问器。）Data is
                typically not copied out of the backing
                buffer.（数据通常不会从底层缓冲区中拷贝出来。）This also
                means the backing buffer must remain alive for the duration of
                the views.（这也意味着，在视图存活期间，底层缓冲区必须保持有效。）</p>
              <h3 id="table-access">Table Access（表访问）</h3>
              <p>If you look in the generated files emitted by
                <code>flatc</code>, you will see it generated
                , for each <code>table</code>, accessors of all its
                non-<code>deprecated</code> fields.（如果你查看 <code>flatc</code>
                生成的文件，会发现它为每个 <code>table（表）</code> 生成了所有非
                <code>deprecated（已弃用）</code> 字段的访问器。）For example,
                some of the accessors of the <code>Monster</code> root table
                would look like:（例如，<code>Monster</code> 根表的一些访问器如下：）</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="13:15"><input
                  checked="checked" id="c_33" name="__tabbed_13"
                  type="radio" /><input id="c_34" name="__tabbed_13"
                  type="radio" /><input id="c_35" name="__tabbed_13"
                  type="radio" /><input id="dart_11" name="__tabbed_13"
                  type="radio" /><input id="go_11" name="__tabbed_13"
                  type="radio" /><input id="java_11" name="__tabbed_13"
                  type="radio" /><input id="javascript_11" name="__tabbed_13"
                  type="radio" /><input id="kotlin_11" name="__tabbed_13"
                  type="radio" /><input id="lobster_11" name="__tabbed_13"
                  type="radio" /><input id="lua_11" name="__tabbed_13"
                  type="radio" /><input id="php_11" name="__tabbed_13"
                  type="radio" /><input id="python_11" name="__tabbed_13"
                  type="radio" /><input id="rust_11" name="__tabbed_13"
                  type="radio" /><input id="swift_11" name="__tabbed_13"
                  type="radio" /><input id="typescript_11" name="__tabbed_13"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_33">C++</label><label for="c_34">C</label><label
                    for="c_35">C#</label><label for="dart_11">Dart</label><label
                    for="go_11">Go</label><label
                    for="java_11">Java</label><label
                    for="javascript_11">JavaScript</label><label
                    for="kotlin_11">Kotlin</label><label
                    for="lobster_11">Lobster</label><label
                    for="lua_11">Lua</label><label
                    for="php_11">PHP</label><label
                    for="python_11">Python</label><label
                    for="rust_11">Rust</label><label
                    for="swift_11">Swift</label><label
                    for="typescript_11">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="k">auto</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="o">-&gt;</span><span class="n">hp</span><span class="p">();</span>
<span class="k">auto</span><span class="w"> </span><span class="n">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="o">-&gt;</span><span class="n">mana</span><span class="p">();</span>
<span class="k">auto</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="o">-&gt;</span><span class="n">name</span><span class="p">()</span><span class="o">-&gt;</span><span class="n">c_str</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">uint16_t</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Monster_hp</span><span class="p">(</span><span class="n">monster</span><span class="p">));</span>
<span class="kt">uint16_t</span><span class="w"> </span><span class="n">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Monster_mana</span><span class="p">(</span><span class="n">monster</span><span class="p">));</span>
<span class="n">flatbuffers_string_t</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Monster_name</span><span class="p">(</span><span class="n">monster</span><span class="p">));</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// For C#, unlike most other languages support by FlatBuffers, most values</span>
<span class="c1">// (except for vectors and unions) are available as properties instead of</span>
<span class="c1">// accessor methods.</span>
<span class="kt">var</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Hp</span><span class="p">;</span>
<span class="kt">var</span><span class="w"> </span><span class="n">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Mana</span><span class="p">;</span>
<span class="kt">var</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Name</span><span class="p">;</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// For Dart, unlike other languages support by FlatBuffers, most values</span>
<span class="c1">// are available as properties instead of accessor methods.</span>
<span class="kd">var</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">hp</span><span class="p">;</span>
<span class="kd">var</span><span class="w"> </span><span class="n">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">mana</span><span class="p">;</span>
<span class="kd">var</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">name</span><span class="p">;</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nx">hp</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">Hp</span><span class="p">()</span>
<span class="nx">mana</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">Mana</span><span class="p">()</span>
<span class="nx">name</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">monster</span><span class="p">.</span><span class="nx">Name</span><span class="p">())</span><span class="w"> </span><span class="c1">// Note: `monster.Name()` returns a byte[].</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">short</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">hp</span><span class="p">();</span>
<span class="kt">short</span><span class="w"> </span><span class="n">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">mana</span><span class="p">();</span>
<span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">name</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">hp</span><span class="p">();</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">mana</span><span class="p">();</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">name</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">val</span><span class="w"> </span><span class="nv">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">hp</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">mana</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">name</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>let hp = monster.hp
let mana = monster.mana
let name = monster.name
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">local</span><span class="w"> </span><span class="nv">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">mon</span><span class="p">:</span><span class="nf">Hp</span><span class="p">()</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">mon</span><span class="p">:</span><span class="nf">Mana</span><span class="p">()</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">mon</span><span class="p">:</span><span class="nf">Name</span><span class="p">()</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nv">$hp</span> <span class="o">=</span> <span class="nv">$monster</span><span class="o">-&gt;</span><span class="na">getHp</span><span class="p">();</span>
<span class="nv">$mana</span> <span class="o">=</span> <span class="nv">$monster</span><span class="o">-&gt;</span><span class="na">getMana</span><span class="p">();</span>
<span class="nv">$name</span> <span class="o">=</span> <span class="nx">monster</span><span class="o">-&gt;</span><span class="na">getName</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">hp</span> <span class="o">=</span> <span class="n">monster</span><span class="o">.</span><span class="n">Hp</span><span class="p">()</span>
<span class="n">mana</span> <span class="o">=</span> <span class="n">monster</span><span class="o">.</span><span class="n">Mana</span><span class="p">()</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">monster</span><span class="o">.</span><span class="n">Name</span><span class="p">()</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Get and test some scalar types from the FlatBuffer.</span>
<span class="kd">let</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">hp</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="n">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">mana</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">name</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="nv">hp</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">hp</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">mana</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">mana</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">name</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">name</span><span class="w"> </span><span class="c1">// returns an optional string</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="nx">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">hp</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">mana</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">mana</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">name</span><span class="p">();</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>These accessors should hold the values <code>300</code>,
                <code>150</code>, and <code>"Orc"</code>
                respectively.（这些访问器分别应当返回 <code>300</code>、<code>150</code> 和
                <code>"Orc"</code> 这几个值。）</p>
              <p>The default value of <code>150</code> wasn't stored in the
                <code>mana</code> field, but we are still
                able to retrieve it.（<code>mana</code> 字段的默认值 <code>150</code>
                实际上并未存储在该字段中，但我们依然能够获取到它。）That is because the generated
                accessors return a hard-coded
                default value when it doesn't find the value in the
                buffer.（这是因为生成的访问器在缓冲区中找不到该值时，会返回一个硬编码的默认值。）</p>
              <h4 id="nested-object-access">Nested Object Access（嵌套对象访问）</h4>
              <p>Accessing nested objects is very similar, with the nested field
                pointing to
                another object type.（访问嵌套对象的方式非常类似，嵌套字段会指向另一个对象类型。）Be careful,
                the field could be <code>null</code> if not
                present.（注意，如果该字段不存在，其值可能为 <code>null</code>。）</p>
              <p>For example, accessing the <code>pos</code>
                <code>struct</code>, which is type <code>Vec3</code> you would
                do:（例如，访问类型为 <code>Vec3</code> 的 <code>pos</code>
                结构体字段时，你可以这样做：）</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="14:15"><input
                  checked="checked" id="c_36" name="__tabbed_14"
                  type="radio" /><input id="c_37" name="__tabbed_14"
                  type="radio" /><input id="c_38" name="__tabbed_14"
                  type="radio" /><input id="dart_12" name="__tabbed_14"
                  type="radio" /><input id="go_12" name="__tabbed_14"
                  type="radio" /><input id="java_12" name="__tabbed_14"
                  type="radio" /><input id="javascript_12" name="__tabbed_14"
                  type="radio" /><input id="kotlin_12" name="__tabbed_14"
                  type="radio" /><input id="lobster_12" name="__tabbed_14"
                  type="radio" /><input id="lua_12" name="__tabbed_14"
                  type="radio" /><input id="php_12" name="__tabbed_14"
                  type="radio" /><input id="python_12" name="__tabbed_14"
                  type="radio" /><input id="rust_12" name="__tabbed_14"
                  type="radio" /><input id="swift_12" name="__tabbed_14"
                  type="radio" /><input id="typescript_12" name="__tabbed_14"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_36">C++</label><label for="c_37">C</label><label
                    for="c_38">C#</label><label for="dart_12">Dart</label><label
                    for="go_12">Go</label><label
                    for="java_12">Java</label><label
                    for="javascript_12">JavaScript</label><label
                    for="kotlin_12">Kotlin</label><label
                    for="lobster_12">Lobster</label><label
                    for="lua_12">Lua</label><label
                    for="php_12">PHP</label><label
                    for="python_12">Python</label><label
                    for="rust_12">Rust</label><label
                    for="swift_12">Swift</label><label
                    for="typescript_12">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="k">auto</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="o">-&gt;</span><span class="n">pos</span><span class="p">();</span>
<span class="k">auto</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="o">-&gt;</span><span class="n">x</span><span class="p">();</span>
<span class="k">auto</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="o">-&gt;</span><span class="n">y</span><span class="p">();</span>
<span class="k">auto</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="o">-&gt;</span><span class="n">z</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">ns</span><span class="p">(</span><span class="n">Vec3_struct_t</span><span class="p">)</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Monster_pos</span><span class="p">(</span><span class="n">monster</span><span class="p">));</span>
<span class="kt">float</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Vec3_x</span><span class="p">(</span><span class="n">pos</span><span class="p">));</span>
<span class="kt">float</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Vec3_y</span><span class="p">(</span><span class="n">pos</span><span class="p">));</span>
<span class="kt">float</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Vec3_z</span><span class="p">(</span><span class="n">pos</span><span class="p">));</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">var</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Pos</span><span class="p">.</span><span class="n">Value</span><span class="p">;</span>
<span class="kt">var</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">X</span><span class="p">;</span>
<span class="kt">var</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">Y</span><span class="p">;</span>
<span class="kt">var</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">Z</span><span class="p">;</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">myGame</span><span class="p">.</span><span class="n">Vec3</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">pos</span><span class="p">;</span>
<span class="kt">double</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">x</span><span class="p">;</span>
<span class="kt">double</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">y</span><span class="p">;</span>
<span class="kt">double</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">z</span><span class="p">;</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nx">pos</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">Pos</span><span class="p">(</span><span class="kc">nil</span><span class="p">)</span>
<span class="nx">x</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">pos</span><span class="p">.</span><span class="nx">X</span><span class="p">()</span>
<span class="nx">y</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">pos</span><span class="p">.</span><span class="nx">Y</span><span class="p">()</span>
<span class="nx">z</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">pos</span><span class="p">.</span><span class="nx">Z</span><span class="p">()</span>

<span class="c1">// Note: Whenever you access a new object, like in `Pos()`, a new temporary</span>
<span class="c1">// accessor object gets created. If your code is very performance sensitive,</span>
<span class="c1">// you can pass in a pointer to an existing `Vec3` instead of `nil`. This</span>
<span class="c1">// allows you to reuse it across many calls to reduce the amount of object</span>
<span class="c1">// allocation/garbage collection.</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">Vec3</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">pos</span><span class="p">();</span>
<span class="kt">float</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="na">x</span><span class="p">();</span>
<span class="kt">float</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="na">y</span><span class="p">();</span>
<span class="kt">float</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="na">z</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">pos</span><span class="p">();</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">pos</span><span class="p">.</span><span class="nx">x</span><span class="p">();</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">pos</span><span class="p">.</span><span class="nx">y</span><span class="p">();</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">pos</span><span class="p">.</span><span class="nx">z</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">val</span><span class="w"> </span><span class="nv">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">pos</span><span class="o">!!</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="na">x</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="na">y</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="na">z</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>let pos = monster.pos
let x = pos.x
let y = pos.y
let z = pos.z
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">local</span><span class="w"> </span><span class="nv">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">mon</span><span class="p">:</span><span class="nf">Pos</span><span class="p">()</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">pos</span><span class="p">:</span><span class="nf">X</span><span class="p">()</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">pos</span><span class="p">:</span><span class="nf">Y</span><span class="p">()</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">pos</span><span class="p">:</span><span class="nf">Z</span><span class="p">()</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nv">$pos</span> <span class="o">=</span> <span class="nv">$monster</span><span class="o">-&gt;</span><span class="na">getPos</span><span class="p">();</span>
<span class="nv">$x</span> <span class="o">=</span> <span class="nv">$pos</span><span class="o">-&gt;</span><span class="na">getX</span><span class="p">();</span>
<span class="nv">$y</span> <span class="o">=</span> <span class="nv">$pos</span><span class="o">-&gt;</span><span class="na">getY</span><span class="p">();</span>
<span class="nv">$z</span> <span class="o">=</span> <span class="nv">$pos</span><span class="o">-&gt;</span><span class="na">getZ</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">pos</span> <span class="o">=</span> <span class="n">monster</span><span class="o">.</span><span class="n">Pos</span><span class="p">()</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">pos</span><span class="o">.</span><span class="n">X</span><span class="p">()</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">pos</span><span class="o">.</span><span class="n">Y</span><span class="p">()</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">pos</span><span class="o">.</span><span class="n">Z</span><span class="p">()</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">pos</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">x</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">y</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">z</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="nv">pos</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">pos</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">x</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">x</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">y</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">y</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">z</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">pos</span><span class="p">.</span><span class="n">z</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="nx">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">pos</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">pos</span><span class="p">.</span><span class="nx">x</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">pos</span><span class="p">.</span><span class="nx">y</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">pos</span><span class="p">.</span><span class="nx">z</span><span class="p">();</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>Where <code>x</code>, <code>y</code>, and <code>z</code> will
                contain <code>1.0</code>, <code>2.0</code>, and <code>3.0</code>
                respectively.（其中 <code>x</code>、<code>y</code> 和 <code>z</code>
                分别包含 <code>1.0</code>、<code>2.0</code> 和 <code>3.0</code>。）</p>
              <h3 id="vector-access">Vector Access（向量访问）</h3>
              <p>Similarly, we can access elements of the <code>inventory</code>
                <code>vector</code> by indexing it.（同样地，我们可以通过索引访问
                <code>inventory（库存）</code> <code>vector（向量）</code> 的元素。）<br>
                You can also iterate over the length of the
                vector.（你也可以遍历该向量的长度。）</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="15:15"><input
                  checked="checked" id="c_39" name="__tabbed_15"
                  type="radio" /><input id="c_40" name="__tabbed_15"
                  type="radio" /><input id="c_41" name="__tabbed_15"
                  type="radio" /><input id="dart_13" name="__tabbed_15"
                  type="radio" /><input id="go_13" name="__tabbed_15"
                  type="radio" /><input id="java_13" name="__tabbed_15"
                  type="radio" /><input id="javascript_13" name="__tabbed_15"
                  type="radio" /><input id="kotlin_13" name="__tabbed_15"
                  type="radio" /><input id="lobster_13" name="__tabbed_15"
                  type="radio" /><input id="lua_13" name="__tabbed_15"
                  type="radio" /><input id="php_13" name="__tabbed_15"
                  type="radio" /><input id="python_13" name="__tabbed_15"
                  type="radio" /><input id="rust_13" name="__tabbed_15"
                  type="radio" /><input id="swift_13" name="__tabbed_15"
                  type="radio" /><input id="typescript_13" name="__tabbed_15"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_39">C++</label><label for="c_40">C</label><label
                    for="c_41">C#</label><label for="dart_13">Dart</label><label
                    for="go_13">Go</label><label
                    for="java_13">Java</label><label
                    for="javascript_13">JavaScript</label><label
                    for="kotlin_13">Kotlin</label><label
                    for="lobster_13">Lobster</label><label
                    for="lua_13">Lua</label><label
                    for="php_13">PHP</label><label
                    for="python_13">Python</label><label
                    for="rust_13">Rust</label><label
                    for="swift_13">Swift</label><label
                    for="typescript_13">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">flatbuffers</span><span class="o">::</span><span class="n">Vector</span><span class="o">&lt;</span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">char</span><span class="o">&gt;</span><span class="w"> </span><span class="n">inv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="o">-&gt;</span><span class="n">inventory</span><span class="p">();</span>
<span class="k">auto</span><span class="w"> </span><span class="n">inv_len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">inv</span><span class="o">-&gt;</span><span class="n">size</span><span class="p">();</span>
<span class="k">auto</span><span class="w"> </span><span class="n">third_item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">inv</span><span class="o">-&gt;</span><span class="n">Get</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// If `inv` hasn&#39;t been set, it will be null. It is valid get</span>
<span class="c1">// the length of null which will be 0, useful for iteration.</span>
<span class="n">flatbuffers_uint8_vec_t</span><span class="w"> </span><span class="n">inv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Monster_inventory</span><span class="p">(</span><span class="n">monster</span><span class="p">));</span>
<span class="kt">size_t</span><span class="w"> </span><span class="n">inv_len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flatbuffers_uint8_vec_len</span><span class="p">(</span><span class="n">inv</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">int</span><span class="w"> </span><span class="n">invLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">InventoryLength</span><span class="p">;</span>
<span class="kt">var</span><span class="w"> </span><span class="n">thirdItem</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Inventory</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">int</span><span class="w"> </span><span class="n">invLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">inventory</span><span class="p">.</span><span class="n">length</span><span class="p">;</span>
<span class="kd">var</span><span class="w"> </span><span class="n">thirdItem</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">inventory</span><span class="p">[</span><span class="m">2</span><span class="p">];</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nx">invLength</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">InventoryLength</span><span class="p">()</span>
<span class="nx">thirdItem</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">Inventory</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">int</span><span class="w"> </span><span class="n">invLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">inventoryLength</span><span class="p">();</span>
<span class="kt">byte</span><span class="w"> </span><span class="n">thirdItem</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">inventory</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">invLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">inventoryLength</span><span class="p">();</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">thirdItem</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">inventory</span><span class="p">(</span><span class="mf">2</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">val</span><span class="w"> </span><span class="nv">invLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">inventoryLength</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">thirdItem</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">inventory</span><span class="p">(</span><span class="m">2</span><span class="p">)</span><span class="o">!!</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>let inv_len = monster.inventory_length
let third_item = monster.inventory(2)
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">local</span><span class="w"> </span><span class="nv">invLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">mon</span><span class="p">:</span><span class="nf">InventoryLength</span><span class="p">()</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">thirdItem</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">mon</span><span class="p">:</span><span class="nf">Inventory</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="c1">-- Lua is 1-based</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nv">$inv_len</span> <span class="o">=</span> <span class="nv">$monster</span><span class="o">-&gt;</span><span class="na">getInventoryLength</span><span class="p">();</span>
<span class="nv">$third_item</span> <span class="o">=</span> <span class="nv">$monster</span><span class="o">-&gt;</span><span class="na">getInventory</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">inv_len</span> <span class="o">=</span> <span class="n">monster</span><span class="o">.</span><span class="n">InventoryLength</span><span class="p">()</span>
<span class="n">third_item</span> <span class="o">=</span> <span class="n">monster</span><span class="o">.</span><span class="n">Inventory</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Get and test an element from the `inventory` FlatBuffer&#39;s `vector`.</span>
<span class="kd">let</span><span class="w"> </span><span class="n">inv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">inventory</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span>

<span class="c1">// Note that this vector is returned as a slice, because direct access for</span>
<span class="c1">// this type, a `u8` vector, is safe on all platforms:</span>
<span class="kd">let</span><span class="w"> </span><span class="n">third_item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">inv</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Get a the count of objects in the vector</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">count</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">inventoryCount</span>

<span class="c1">// get item at index 4</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">object</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">inventory</span><span class="p">(</span><span class="n">at</span><span class="p">:</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span>

<span class="c1">// or you can fetch the entire array</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">inv</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">inventory</span>
<span class="c1">// inv[4] should equal object</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="nx">invLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">inventoryLength</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">thirdItem</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">inventory</span><span class="p">(</span><span class="mf">2</span><span class="p">);</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <p>For vectors of tables, you can access the elements like any
                other vector, except
                you need to handle the result as a FlatBuffer
                table.（对于表（table）类型的向量，你可以像访问其他向量一样访问其元素，只不过需要将结果作为 FlatBuffer
                表来处理。）Here we iterate over the
                <code>weapons</code> vector that is houses <code>Weapon</code>
                <code>tables</code>.（这里我们遍历包含 <code>Weapon（武器）</code>
                <code>tables（表）</code> 的 <code>weapons（武器）</code> 向量。）</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="16:15"><input
                  checked="checked" id="c_42" name="__tabbed_16"
                  type="radio" /><input id="c_43" name="__tabbed_16"
                  type="radio" /><input id="c_44" name="__tabbed_16"
                  type="radio" /><input id="dart_14" name="__tabbed_16"
                  type="radio" /><input id="go_14" name="__tabbed_16"
                  type="radio" /><input id="java_14" name="__tabbed_16"
                  type="radio" /><input id="javascript_14" name="__tabbed_16"
                  type="radio" /><input id="kotlin_14" name="__tabbed_16"
                  type="radio" /><input id="lobster_14" name="__tabbed_16"
                  type="radio" /><input id="lua_14" name="__tabbed_16"
                  type="radio" /><input id="php_14" name="__tabbed_16"
                  type="radio" /><input id="python_14" name="__tabbed_16"
                  type="radio" /><input id="rust_14" name="__tabbed_16"
                  type="radio" /><input id="swift_14" name="__tabbed_16"
                  type="radio" /><input id="typescript_14" name="__tabbed_16"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_42">C++</label><label for="c_43">C</label><label
                    for="c_44">C#</label><label for="dart_14">Dart</label><label
                    for="go_14">Go</label><label
                    for="java_14">Java</label><label
                    for="javascript_14">JavaScript</label><label
                    for="kotlin_14">Kotlin</label><label
                    for="lobster_14">Lobster</label><label
                    for="lua_14">Lua</label><label
                    for="php_14">PHP</label><label
                    for="python_14">Python</label><label
                    for="rust_14">Rust</label><label
                    for="swift_14">Swift</label><label
                    for="typescript_14">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">flatbuffers</span><span class="o">::</span><span class="n">Vector</span><span class="o">&lt;</span><span class="n">Weapon</span><span class="o">&gt;</span><span class="w"> </span><span class="n">weapons</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="o">-&gt;</span><span class="n">weapons</span><span class="p">();</span>
<span class="k">auto</span><span class="w"> </span><span class="n">weapon_len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapons</span><span class="o">-&gt;</span><span class="n">size</span><span class="p">();</span>
<span class="k">auto</span><span class="w"> </span><span class="n">second_weapon_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapons</span><span class="o">-&gt;</span><span class="n">Get</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">-&gt;</span><span class="n">name</span><span class="p">()</span><span class="o">-&gt;</span><span class="n">str</span><span class="p">();</span>
<span class="k">auto</span><span class="w"> </span><span class="n">second_weapon_damage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapons</span><span class="o">-&gt;</span><span class="n">Get</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">-&gt;</span><span class="n">damage</span><span class="p">()</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">ns</span><span class="p">(</span><span class="n">Weapon_vec_t</span><span class="p">)</span><span class="w"> </span><span class="n">weapons</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Monster_weapons</span><span class="p">(</span><span class="n">monster</span><span class="p">));</span>
<span class="kt">size_t</span><span class="w"> </span><span class="n">weapons_len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Weapon_vec_len</span><span class="p">(</span><span class="n">weapons</span><span class="p">));</span>
<span class="c1">// We can use `const char *` instead of `flatbuffers_string_t`.</span>
<span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">second_weapon_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>
<span class="w">    </span><span class="n">ns</span><span class="p">(</span><span class="n">Weapon_name</span><span class="p">(</span><span class="n">ns</span><span class="p">(</span><span class="n">Weapon_vec_at</span><span class="p">(</span><span class="n">weapons</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">))));</span>
<span class="kt">uint16_t</span><span class="w"> </span><span class="n">second_weapon_damage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>
<span class="w">    </span><span class="n">ns</span><span class="p">(</span><span class="n">Weapon_damage</span><span class="p">(</span><span class="n">ns</span><span class="p">(</span><span class="n">Weapon_vec_at</span><span class="p">(</span><span class="n">weapons</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">))));</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">int</span><span class="w"> </span><span class="n">weaponsLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">WeaponsLength</span><span class="p">;</span>
<span class="kt">var</span><span class="w"> </span><span class="n">secondWeaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Weapons</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="n">Name</span><span class="p">;</span>
<span class="kt">var</span><span class="w"> </span><span class="n">secondWeaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Weapons</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="n">Damage</span><span class="p">;</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">int</span><span class="w"> </span><span class="n">weaponsLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">weapons</span><span class="p">.</span><span class="n">length</span><span class="p">;</span>
<span class="kd">var</span><span class="w"> </span><span class="n">secondWeaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">weapons</span><span class="p">[</span><span class="m">1</span><span class="p">].</span><span class="n">name</span><span class="p">;</span>
<span class="kd">var</span><span class="w"> </span><span class="n">secondWeaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Weapons</span><span class="p">[</span><span class="m">1</span><span class="p">].</span><span class="n">damage</span><span class="p">;</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nx">weaponLength</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">WeaponsLength</span><span class="p">()</span>
<span class="c1">// We need a `sample.Weapon` to pass into `monster.Weapons()`</span>
<span class="c1">// to capture the output of the function.k</span>
<span class="nx">weapon</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">new</span><span class="p">(</span><span class="nx">sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">)</span>
<span class="k">if</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">Weapons</span><span class="p">(</span><span class="nx">weapon</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">secondWeaponName</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">weapon</span><span class="p">.</span><span class="nx">Name</span><span class="p">()</span>
<span class="w">        </span><span class="nx">secondWeaponDamage</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">weapon</span><span class="p">.</span><span class="nx">Damage</span><span class="p">()</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">int</span><span class="w"> </span><span class="n">weaponsLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">weaponsLength</span><span class="p">();</span>
<span class="n">String</span><span class="w"> </span><span class="n">secondWeaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">weapons</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="na">name</span><span class="p">();</span>
<span class="kt">short</span><span class="w"> </span><span class="n">secondWeaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">weapons</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="na">damage</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">weaponsLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">weaponsLength</span><span class="p">();</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">secondWeaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">weapons</span><span class="p">(</span><span class="mf">1</span><span class="p">).</span><span class="nx">name</span><span class="p">();</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">secondWeaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">weapons</span><span class="p">(</span><span class="mf">1</span><span class="p">).</span><span class="nx">damage</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">val</span><span class="w"> </span><span class="nv">weaponsLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">weaponsLength</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">secondWeaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">weapons</span><span class="p">(</span><span class="m">1</span><span class="p">)</span><span class="o">!!</span><span class="p">.</span><span class="na">name</span>
<span class="kd">val</span><span class="w"> </span><span class="nv">secondWeaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">weapons</span><span class="p">(</span><span class="m">1</span><span class="p">)</span><span class="o">!!</span><span class="p">.</span><span class="na">damage</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>let weapons_length = monster.weapons_length
let second_weapon_name = monster.weapons(1).name
let second_weapon_damage = monster.weapons(1).damage
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">local</span><span class="w"> </span><span class="nv">weaponsLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">mon</span><span class="p">:</span><span class="nf">WeaponsLength</span><span class="p">()</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">secondWeaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">mon</span><span class="p">:</span><span class="nf">Weapon</span><span class="p">(</span><span class="mi">2</span><span class="p">):</span><span class="nf">Name</span><span class="p">()</span>
<span class="kd">local</span><span class="w"> </span><span class="nv">secondWeaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">mon</span><span class="p">:</span><span class="nf">Weapon</span><span class="p">(</span><span class="mi">2</span><span class="p">):</span><span class="nf">Damage</span><span class="p">()</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nv">$weapons_len</span> <span class="o">=</span> <span class="nv">$monster</span><span class="o">-&gt;</span><span class="na">getWeaponsLength</span><span class="p">();</span>
<span class="nv">$second_weapon_name</span> <span class="o">=</span> <span class="nv">$monster</span><span class="o">-&gt;</span><span class="na">getWeapons</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">getName</span><span class="p">();</span>
<span class="nv">$second_weapon_damage</span> <span class="o">=</span> <span class="nv">$monster</span><span class="o">-&gt;</span><span class="na">getWeapons</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">getDamage</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">weapons_length</span> <span class="o">=</span> <span class="n">monster</span><span class="o">.</span><span class="n">WeaponsLength</span><span class="p">()</span>
<span class="n">second_weapon_name</span> <span class="o">=</span> <span class="n">monster</span><span class="o">.</span><span class="n">Weapons</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">Name</span><span class="p">()</span>
<span class="n">second_weapon_damage</span> <span class="o">=</span> <span class="n">monster</span><span class="o">.</span><span class="n">Weapons</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">Damage</span><span class="p">()</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Get and test the `weapons` FlatBuffers&#39;s `vector`.</span>
<span class="kd">let</span><span class="w"> </span><span class="n">weps</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">weapons</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="n">weps_len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weps</span><span class="p">.</span><span class="n">len</span><span class="p">();</span>

<span class="kd">let</span><span class="w"> </span><span class="n">wep2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weps</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="kd">let</span><span class="w"> </span><span class="n">second_weapon_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">wep2</span><span class="p">.</span><span class="n">name</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="n">second_weapon_damage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">wep2</span><span class="p">.</span><span class="n">damage</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Get the count of weapon objects</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">wepsCount</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">weaponsCount</span>

<span class="kd">let</span><span class="w"> </span><span class="nv">weapon2</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">weapons</span><span class="p">(</span><span class="n">at</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">weaponName</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">weapon2</span><span class="p">.</span><span class="n">name</span>
<span class="kd">let</span><span class="w"> </span><span class="nv">weaponDmg</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">weapon2</span><span class="p">.</span><span class="n">damage</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="nx">weaponsLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">weaponsLength</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">secondWeaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">weapons</span><span class="p">(</span><span class="mf">1</span><span class="p">).</span><span class="nx">name</span><span class="p">();</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">secondWeaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">weapons</span><span class="p">(</span><span class="mf">1</span><span class="p">).</span><span class="nx">damage</span><span class="p">();</span>
</code></pre></div>
                  </div>
                </div>
              </div>
              <h3 id="union-access">Union Access（联合体访问）</h3>
              <p>Lastly , we can access our <code>equipped</code>
                <code>union</code> field.（最后，我们可以访问 <code>equipped（装备）</code>
                <code>union（联合体）</code> 字段。）Just like when we created
                the union, we need to get both parts of the union: the type and
                the data.（与创建联合体时一样，我们需要获取联合体的两个部分：类型和值数据。）</p>
              <p>We can access the type to dynamically cast the data as needed
                (since the union
                only stores a FlatBuffer
                <code>table</code>).（我们可以访问类型，以便根据需要动态转换数据类型（因为联合体只存储 FlatBuffer
                <code>table（表）</code>）。）</p>
              <div class="tabbed-set tabbed-alternate" data-tabs="17:15"><input
                  checked="checked" id="c_45" name="__tabbed_17"
                  type="radio" /><input id="c_46" name="__tabbed_17"
                  type="radio" /><input id="c_47" name="__tabbed_17"
                  type="radio" /><input id="dart_15" name="__tabbed_17"
                  type="radio" /><input id="go_15" name="__tabbed_17"
                  type="radio" /><input id="java_15" name="__tabbed_17"
                  type="radio" /><input id="javascript_15" name="__tabbed_17"
                  type="radio" /><input id="kotlin_15" name="__tabbed_17"
                  type="radio" /><input id="lobster_15" name="__tabbed_17"
                  type="radio" /><input id="lua_15" name="__tabbed_17"
                  type="radio" /><input id="php_15" name="__tabbed_17"
                  type="radio" /><input id="python_15" name="__tabbed_17"
                  type="radio" /><input id="rust_15" name="__tabbed_17"
                  type="radio" /><input id="swift_15" name="__tabbed_17"
                  type="radio" /><input id="typescript_15" name="__tabbed_17"
                  type="radio" /><div class="tabbed-labels"><label
                    for="c_45">C++</label><label for="c_46">C</label><label
                    for="c_47">C#</label><label for="dart_15">Dart</label><label
                    for="go_15">Go</label><label
                    for="java_15">Java</label><label
                    for="javascript_15">JavaScript</label><label
                    for="kotlin_15">Kotlin</label><label
                    for="lobster_15">Lobster</label><label
                    for="lua_15">Lua</label><label
                    for="php_15">PHP</label><label
                    for="python_15">Python</label><label
                    for="rust_15">Rust</label><label
                    for="swift_15">Swift</label><label
                    for="typescript_15">TypeScript</label></div>
                <div class="tabbed-content">
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="k">auto</span><span class="w"> </span><span class="n">union_type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">equipped_type</span><span class="p">();</span>

<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">union_type</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">Equipment_Weapon</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">     </span><span class="c1">// Requires `static_cast` to type `const Weapon*`.</span>
<span class="w">    </span><span class="k">auto</span><span class="w"> </span><span class="n">weapon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">static_cast</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">Weapon</span><span class="o">*&gt;</span><span class="p">(</span><span class="n">monster</span><span class="o">-&gt;</span><span class="n">equipped</span><span class="p">());</span>

<span class="w">    </span><span class="k">auto</span><span class="w"> </span><span class="n">weapon_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapon</span><span class="o">-&gt;</span><span class="n">name</span><span class="p">()</span><span class="o">-&gt;</span><span class="n">str</span><span class="p">();</span><span class="w"> </span><span class="c1">// &quot;Axe&quot;</span>
<span class="w">    </span><span class="k">auto</span><span class="w"> </span><span class="n">weapon_damage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapon</span><span class="o">-&gt;</span><span class="n">damage</span><span class="p">();</span><span class="w">    </span><span class="c1">// 5</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Access union type field.</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">ns</span><span class="p">(</span><span class="n">Monster_equipped_type</span><span class="p">(</span><span class="n">monster</span><span class="p">))</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Equipment_Weapon</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="c1">// Cast to appropriate type:</span>
<span class="w">    </span><span class="c1">// C allows for silent void pointer assignment, so we need no </span>
<span class="w">    </span><span class="c1">// explicit cast.</span>
<span class="w">    </span><span class="n">ns</span><span class="p">(</span><span class="n">Weapon_table_t</span><span class="p">)</span><span class="w"> </span><span class="n">weapon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Monster_equipped</span><span class="p">(</span><span class="n">monster</span><span class="p">));</span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">weapon_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Weapon_name</span><span class="p">(</span><span class="n">weapon</span><span class="p">));</span><span class="w"> </span><span class="c1">// &quot;Axe&quot;</span>
<span class="w">    </span><span class="kt">uint16_t</span><span class="w"> </span><span class="n">weapon_damage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ns</span><span class="p">(</span><span class="n">Weapon_damage</span><span class="p">(</span><span class="n">weapon</span><span class="p">));</span><span class="w"> </span><span class="c1">// 5</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">var</span><span class="w"> </span><span class="n">unionType</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">EquippedType</span><span class="p">;</span>

<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">unionType</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">Equipment</span><span class="p">.</span><span class="n">Weapon</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kt">var</span><span class="w"> </span><span class="n">weapon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Equipped</span><span class="o">&lt;</span><span class="n">Weapon</span><span class="o">&gt;</span><span class="p">().</span><span class="n">Value</span><span class="p">;</span>

<span class="w">    </span><span class="kt">var</span><span class="w"> </span><span class="n">weaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapon</span><span class="p">.</span><span class="n">Name</span><span class="p">;</span><span class="w">     </span><span class="c1">// &quot;Axe&quot;</span>
<span class="w">    </span><span class="kt">var</span><span class="w"> </span><span class="n">weaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapon</span><span class="p">.</span><span class="n">Damage</span><span class="p">;</span><span class="w"> </span><span class="c1">// 5</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="n">unionType</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">equippedType</span><span class="p">.</span><span class="n">value</span><span class="p">;</span>

<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">unionType</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">myGame</span><span class="p">.</span><span class="n">EquipmentTypeId</span><span class="p">.</span><span class="n">Weapon</span><span class="p">.</span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="n">myGame</span><span class="p">.</span><span class="n">Weapon</span><span class="w"> </span><span class="n">weapon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mon</span><span class="p">.</span><span class="n">equipped</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">myGame</span><span class="p">.</span><span class="n">Weapon</span><span class="p">;</span>

<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="n">weaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapon</span><span class="p">.</span><span class="n">name</span><span class="p">;</span><span class="w">     </span><span class="c1">// &quot;Axe&quot;</span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="n">weaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapon</span><span class="p">.</span><span class="n">damage</span><span class="p">;</span><span class="w"> </span><span class="c1">// 5</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// We need a `flatbuffers.Table` to capture the output of the</span>
<span class="c1">// `monster.Equipped()` function.</span>
<span class="nx">unionTable</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">new</span><span class="p">(</span><span class="nx">flatbuffers</span><span class="p">.</span><span class="nx">Table</span><span class="p">)</span>

<span class="k">if</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">Equipped</span><span class="p">(</span><span class="nx">unionTable</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">unionType</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">EquippedType</span><span class="p">()</span>

<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">unionType</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">sample</span><span class="p">.</span><span class="nx">EquipmentWeapon</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="c1">// Create a `sample.Weapon` object that can be initialized with the </span>
<span class="w">        </span><span class="c1">// contents of the `flatbuffers.Table` (`unionTable`), which was</span>
<span class="w">        </span><span class="c1">// populated by `monster.Equipped()`.</span>
<span class="w">        </span><span class="nx">unionWeapon</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">new</span><span class="p">(</span><span class="nx">sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">)</span>
<span class="w">        </span><span class="nx">unionWeapon</span><span class="p">.</span><span class="nx">Init</span><span class="p">(</span><span class="nx">unionTable</span><span class="p">.</span><span class="nx">Bytes</span><span class="p">,</span><span class="w"> </span><span class="nx">unionTable</span><span class="p">.</span><span class="nx">Pos</span><span class="p">)</span>

<span class="w">        </span><span class="nx">weaponName</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">unionWeapon</span><span class="p">.</span><span class="nx">Name</span><span class="p">()</span>
<span class="w">        </span><span class="nx">weaponDamage</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">unionWeapon</span><span class="p">.</span><span class="nx">Damage</span><span class="p">()</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kt">int</span><span class="w"> </span><span class="n">unionType</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">EquippedType</span><span class="p">();</span>

<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">unionType</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">Equipment</span><span class="p">.</span><span class="na">Weapon</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="c1">// Requires an explicit cast to `Weapon`.</span>
<span class="w">    </span><span class="n">Weapon</span><span class="w"> </span><span class="n">weapon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Weapon</span><span class="p">)</span><span class="n">monster</span><span class="p">.</span><span class="na">equipped</span><span class="p">(</span><span class="k">new</span><span class="w"> </span><span class="n">Weapon</span><span class="p">());</span>

<span class="w">    </span><span class="n">String</span><span class="w"> </span><span class="n">weaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapon</span><span class="p">.</span><span class="na">name</span><span class="p">();</span><span class="w">    </span><span class="c1">// &quot;Axe&quot;</span>
<span class="w">    </span><span class="kt">short</span><span class="w"> </span><span class="n">weaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapon</span><span class="p">.</span><span class="na">damage</span><span class="p">();</span><span class="w"> </span><span class="c1">// 5</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">unionType</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">equippedType</span><span class="p">();</span>

<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">unionType</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Equipment</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="c1">// &#39;Axe&#39;</span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="nx">weaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">equipped</span><span class="p">(</span><span class="ow">new</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">()).</span><span class="nx">name</span><span class="p">();</span>
<span class="w">    </span><span class="c1">// 5</span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="nx">weaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>
<span class="w">        </span><span class="nx">monster</span><span class="p">.</span><span class="nx">equipped</span><span class="p">(</span><span class="ow">new</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">()).</span><span class="nx">damage</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">val</span><span class="w"> </span><span class="nv">unionType</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">EquippedType</span>

<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">unionType</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">Equipment</span><span class="p">.</span><span class="na">Weapon</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="c1">// Requires an explicit cast to `Weapon`.</span>
<span class="w">    </span><span class="kd">val</span><span class="w"> </span><span class="nv">weapon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="na">equipped</span><span class="p">(</span><span class="n">Weapon</span><span class="p">())</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">Weapon</span>

<span class="w">    </span><span class="kd">val</span><span class="w"> </span><span class="nv">weaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapon</span><span class="p">.</span><span class="na">name</span><span class="w">   </span><span class="c1">// &quot;Axe&quot;</span>
<span class="w">    </span><span class="kd">val</span><span class="w"> </span><span class="nv">weaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">weapon</span><span class="p">.</span><span class="na">damage</span><span class="w"> </span><span class="c1">// 5</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code>union_type = monster.equipped_type

if union_type == MyGame_Sample_Equipment_Weapon:
    // `monster.equipped_as_Weapon` returns a FlatBuffer handle much like 
    // normal table fields, but this is only valid to call if we already 
    // know it is the correct type.
    let union_weapon = monster.equipped_as_Weapon

    let weapon_name = union_weapon.name     // &quot;Axe&quot;
    let weapon_damage = union_weapon.damage // 5
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">local</span><span class="w"> </span><span class="nv">unionType</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">mon</span><span class="p">:</span><span class="nf">EquippedType</span><span class="p">()</span>

<span class="kr">if</span><span class="w"> </span><span class="nv">unionType</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nv">equipment</span><span class="p">.</span><span class="py">Weapon</span><span class="w"> </span><span class="kr">then</span>
<span class="w">    </span><span class="kd">local</span><span class="w"> </span><span class="nv">unionWeapon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">weapon</span><span class="p">.</span><span class="nf">New</span><span class="p">()</span>
<span class="w">    </span><span class="nv">unionWeapon</span><span class="p">:</span><span class="nf">Init</span><span class="p">(</span><span class="nv">mon</span><span class="p">:</span><span class="nf">Equipped</span><span class="p">().</span><span class="nv">bytes</span><span class="p">,</span><span class="w"> </span><span class="nv">mon</span><span class="p">:</span><span class="nf">Equipped</span><span class="p">().</span><span class="nv">pos</span><span class="p">)</span>

<span class="w">    </span><span class="kd">local</span><span class="w"> </span><span class="nv">weaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">unionWeapon</span><span class="p">:</span><span class="nf">Name</span><span class="p">()</span><span class="w">     </span><span class="c1">-- &#39;Axe&#39;</span>
<span class="w">    </span><span class="kd">local</span><span class="w"> </span><span class="nv">weaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">unionWeapon</span><span class="p">:</span><span class="nf">Damage</span><span class="p">()</span><span class="w"> </span><span class="c1">-- 5</span>
<span class="kr">end</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="nv">$union_type</span> <span class="o">=</span> <span class="nv">$monster</span><span class="o">-&gt;</span><span class="na">getEquippedType</span><span class="p">();</span>

<span class="k">if</span> <span class="p">(</span><span class="nv">$union_type</span> <span class="o">==</span> <span class="nx">\MyGame\Sample\Equipment</span><span class="o">::</span><span class="na">Weapon</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// &quot;Axe&quot;</span>
    <span class="nv">$weapon_name</span> <span class="o">=</span>
        <span class="nv">$monster</span><span class="o">-&gt;</span><span class="na">getEquipped</span><span class="p">(</span><span class="k">new</span> <span class="nx">\MyGame\Sample\Weapon</span><span class="p">())</span><span class="o">-&gt;</span><span class="na">getName</span><span class="p">();</span>
    <span class="c1">// 5</span>
    <span class="nv">$weapon_damage</span> <span class="o">=</span> 
        <span class="nv">$monster</span><span class="o">-&gt;</span><span class="na">getEquipped</span><span class="p">(</span><span class="k">new</span> <span class="nx">\MyGame\Sample\Weapon</span><span class="p">())</span><span class="o">-&gt;</span><span class="na">getDamage</span><span class="p">();</span> 
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="n">union_type</span> <span class="o">=</span> <span class="n">monster</span><span class="o">.</span><span class="n">EquippedType</span><span class="p">()</span>

<span class="k">if</span> <span class="n">union_type</span> <span class="o">==</span> <span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Equipment</span><span class="o">.</span><span class="n">Equipment</span><span class="p">()</span><span class="o">.</span><span class="n">Weapon</span><span class="p">:</span>
    <span class="c1"># `monster.Equipped()` returns a `flatbuffers.Table`, which can be used</span>
    <span class="c1"># to initialize a `MyGame.Sample.Weapon.Weapon()`.</span>
    <span class="n">union_weapon</span> <span class="o">=</span> <span class="n">MyGame</span><span class="o">.</span><span class="n">Sample</span><span class="o">.</span><span class="n">Weapon</span><span class="o">.</span><span class="n">Weapon</span><span class="p">()</span>
    <span class="n">union_weapon</span><span class="o">.</span><span class="n">Init</span><span class="p">(</span><span class="n">monster</span><span class="o">.</span><span class="n">Equipped</span><span class="p">()</span><span class="o">.</span><span class="n">Bytes</span><span class="p">,</span> <span class="n">monster</span><span class="o">.</span><span class="n">Equipped</span><span class="p">()</span><span class="o">.</span><span class="n">Pos</span><span class="p">)</span>

    <span class="n">weapon_name</span> <span class="o">=</span> <span class="n">union_weapon</span><span class="o">.</span><span class="n">Name</span><span class="p">()</span>     <span class="o">//</span> <span class="s1">&#39;Axe&#39;</span>
    <span class="n">weapon_damage</span> <span class="o">=</span> <span class="n">union_weapon</span><span class="o">.</span><span class="n">Damage</span><span class="p">()</span> <span class="o">//</span> <span class="mi">5</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Get and test the `Equipment` union (`equipped` field).</span>
<span class="c1">// `equipped_as_weapon` returns a FlatBuffer handle much like normal table</span>
<span class="c1">// fields, but this will return `None` if the union is not actually of that</span>
<span class="c1">// type.</span>
<span class="k">if</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">equipped_type</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">Equipment</span><span class="p">::</span><span class="n">Weapon</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">equipped</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">equipped_as_weapon</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">weapon_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">equipped</span><span class="p">.</span><span class="n">name</span><span class="p">();</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">weapon_damage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">equipped</span><span class="p">.</span><span class="n">damage</span><span class="p">();</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="c1">// Get and check if the monster has an equipped item</span>
<span class="k">if</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">equippedType</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="p">.</span><span class="n">weapon</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nv">_weapon</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">equipped</span><span class="p">(</span><span class="n">type</span><span class="p">:</span><span class="w"> </span><span class="n">Weapon</span><span class="p">.</span><span class="kc">self</span><span class="p">)</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nv">name</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">_weapon</span><span class="p">.</span><span class="n">name</span><span class="w"> </span><span class="c1">// should return &quot;Axe&quot;</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nv">dmg</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">_weapon</span><span class="p">.</span><span class="n">damage</span><span class="w"> </span><span class="c1">// should return 5</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                  <div class="tabbed-block">
                    <div
                      class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="nx">unionType</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">equippedType</span><span class="p">();</span>

<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">unionType</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Equipment</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="c1">// &#39;Axe&#39;</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">weaponName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">equipped</span><span class="p">(</span><span class="ow">new</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">()).</span><span class="nx">name</span><span class="p">();</span><span class="w">    </span>
<span class="w">    </span><span class="c1">// 5 </span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">weaponDamage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">monster</span><span class="p">.</span><span class="nx">equipped</span><span class="p">(</span><span class="ow">new</span><span class="w"> </span><span class="nx">MyGame</span><span class="p">.</span><span class="nx">Sample</span><span class="p">.</span><span class="nx">Weapon</span><span class="p">()).</span><span class="nx">damage</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
                  </div>
                </div>
              </div>

            </article>
          </div>

          <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script>

          <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script>
        </div>

      </main>

      <footer class="md-footer">

        <nav class="md-footer__inner md-grid" aria-label="Footer">

          <a href="../quick_start/index.html"
            class="md-footer__link md-footer__link--prev"
            aria-label="Previous: Quick Start（快速入门）">
            <div class="md-footer__button md-icon">

              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path
                  d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z" /></svg>
            </div>
            <div class="md-footer__title">
              <span class="md-footer__direction">
                Previous
              </span>
              <div class="md-ellipsis">
                Quick Start（快速入门）
              </div>
            </div>
          </a>

          <a href="../building/index.html"
            class="md-footer__link md-footer__link--next"
            aria-label="Next: Building">
            <div class="md-footer__title">
              <span class="md-footer__direction">
                Next
              </span>
              <div class="md-ellipsis">
                Building
              </div>
            </div>
            <div class="md-footer__button md-icon">

              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path
                  d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11z" /></svg>
            </div>
          </a>

        </nav>

        <div class="md-footer-meta md-typeset">
          <div class="md-footer-meta__inner md-grid">
            <div class="md-copyright">

              <div class="md-copyright__highlight">
                Copyright &copy; 2025 Google
              </div>

              Made with
              <a href="https://squidfunk.github.io/mkdocs-material/"
                target="_blank" rel="noopener">
                Material for MkDocs
              </a>

            </div>

            <div class="md-social">

              <a href="https://github.com/google/flatbuffers" target="_blank"
                rel="noopener" title="github.com" class="md-social__link">
                <svg xmlns="http://www.w3.org/2000/svg"
                  viewBox="0 0 496 512"><!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path
                    d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8M97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2" /></svg>
              </a>

              <a href="https:///discord.gg/6qgKs3R" target="_blank"
                rel="noopener" title class="md-social__link">
                <svg xmlns="http://www.w3.org/2000/svg"
                  viewBox="0 0 640 512"><!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path
                    d="M524.531 69.836a1.5 1.5 0 0 0-.764-.7A485 485 0 0 0 404.081 32.03a1.82 1.82 0 0 0-1.923.91 338 338 0 0 0-14.9 30.6 447.9 447.9 0 0 0-134.426 0 310 310 0 0 0-15.135-30.6 1.89 1.89 0 0 0-1.924-.91 483.7 483.7 0 0 0-119.688 37.107 1.7 1.7 0 0 0-.788.676C39.068 183.651 18.186 294.69 28.43 404.354a2.02 2.02 0 0 0 .765 1.375 487.7 487.7 0 0 0 146.825 74.189 1.9 1.9 0 0 0 2.063-.676A348 348 0 0 0 208.12 430.4a1.86 1.86 0 0 0-1.019-2.588 321 321 0 0 1-45.868-21.853 1.885 1.885 0 0 1-.185-3.126 251 251 0 0 0 9.109-7.137 1.82 1.82 0 0 1 1.9-.256c96.229 43.917 200.41 43.917 295.5 0a1.81 1.81 0 0 1 1.924.233 235 235 0 0 0 9.132 7.16 1.884 1.884 0 0 1-.162 3.126 301.4 301.4 0 0 1-45.89 21.83 1.875 1.875 0 0 0-1 2.611 391 391 0 0 0 30.014 48.815 1.86 1.86 0 0 0 2.063.7A486 486 0 0 0 610.7 405.729a1.88 1.88 0 0 0 .765-1.352c12.264-126.783-20.532-236.912-86.934-334.541M222.491 337.58c-28.972 0-52.844-26.587-52.844-59.239s23.409-59.241 52.844-59.241c29.665 0 53.306 26.82 52.843 59.239 0 32.654-23.41 59.241-52.843 59.241m195.38 0c-28.971 0-52.843-26.587-52.843-59.239s23.409-59.241 52.843-59.241c29.667 0 53.307 26.82 52.844 59.239 0 32.654-23.177 59.241-52.844 59.241" /></svg>
              </a>

              <a href="https://twitter.com/dbaileychess" target="_blank"
                rel="noopener" title="twitter.com" class="md-social__link">
                <svg xmlns="http://www.w3.org/2000/svg"
                  viewBox="0 0 512 512"><!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path
                    d="M389.2 48h70.6L305.6 224.2 487 464H345L233.7 318.6 106.5 464H35.8l164.9-188.5L26.8 48h145.6l100.5 132.9zm-24.8 373.8h39.1L151.1 88h-42z" /></svg>
              </a>

            </div>

          </div>
        </div>
      </footer>

    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>

    <script id="__config"
      type="application/json">{"base": "..", "features": ["content.code.annotate", "content.tabs.link", "navigation.expand", "navigation.footer", "header.autohide", "content.action.edit"], "search": "../assets/javascripts/workers/search.f8cc74c7.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script>

    <script src="../assets/javascripts/bundle.c8b220af.min.js"></script>

  </body>

  <!-- Mirrored from flatbuffers.dev/tutorial/ by HTTrack Website Copier/3.x [XR&CO'2014], Thu, 08 May 2025 07:01:57 GMT -->
</html>